ruby on rails - "Error": errors.add don't show only the message in locales/xx.yml -
i have method in model check if 1 suspicious attachment before make upload , in case , if ends . bat . . exe , . src or. cmd .
i want show message if file suspicious . i'm portuguese , use 1 file of translate .
the method is:
def suspicious_attachment if ends_with? '.bat', '.com', '.exe', '.src', '.cmd' errors.add(:attachment_file_name, i18n.t('errors.messages.suspicious_attachment', :value => attachment.path[-4..-1])) errors.add_to_base(i18n.t('errors.messages.suspicious_attachment', :value => attachment.path[-4..-1])) errors.add(:attachment_file_name) end end which returns :
attachments attachment file name not allowed upload . attachments attachment file name not valid attachments base not allowed upload . with
i not want show words : "attachments attachment file name" , "attachments base ."
i not understand why these words appear.
sorry english.
thanks
these string displayed because added error message on attributes, not base:
errors.add(:base, "some custom error message") will display message like:
"some custom error message"
whereas this
errors.add(:attribute, "other message") will display message like:
"attribute" other message"
in case, use :base add errors:
def suspitious_attachment if ends_with? '.bat', '.com', '.exe', '.src', '.cmd' errors.add(:base, i18n.t('errors.messages.suspitious_attachment', :value => attachment.path[-4..-1])) end end or if want translate attribute message:
activerecords: attributes: your_model_name: attachment_file_name: "file" and use add corresponding message:
def suspitious_attachment if ends_with? '.bat', '.com', '.exe', '.src', '.cmd' errors.add(:attachment_file_name, i18n.t('errors.messages.suspitious_attachment', :value => attachment.path[-4..-1])) end end which should display error this:
"file" not allowed upload
Comments
Post a Comment