Reputation: 450
To be specific I was trying to localize the Custom Methods example here. I found a similar question but I don't know how to pass that :message argument in errors.add() method. I tried doing something like this:
errors.add(:discount, message: :greater_than_value_error)
but it prints:
{:message=>:greater_than_value_error}
instead of printing the actual error message that was in the .yml file.
What should be the correct syntax here?
My .yml file looke like this (not sure whether this piece below is 100% accurate):
activerecord: #try to use activemodel here
errors:
models:
invoice: # or namespace/model_name
attributes:
discount:
greater_than_value_error: "can't be greater than total value"
Upvotes: 1
Views: 2719
Reputation: 8065
If you are writing custom validation then you need do actual translation,
errors.add(:discount, I18n.t(path_to_locale_text))
You can internationalize the standard validation messages like predefined validators greater_than_value_error
only if you are using built in methods.
validates_numericality_of :discount :greater_than => limit
Check https://stackoverflow.com/a/4453350/1232447 for more details.
Upvotes: -1
Reputation: 2541
try following errors.add :field_name, :message
with rails 4.
errors.add(:discount, :greater_than_value_error)
Upvotes: 4