Validation error messages, setting attribute name

If the model has an attribute named "unit", for example, but in your views you refer to this attribute as "unit price", but when you do the check, the default error messages are "unit", how do I change this to say "price for unit "?

+3
source share
2 answers

Use localization to set the "English" name of your attribute. You can set both exclusive and plural names:

en:
  activerecord:
    attributes:
      product:
        unit:
          one:   Unit price
          other: Unit prices
+2
source

I'm not sure how you can change the column name, but the next workflow

in your model create a virtual attribute unit_price

-

attr_accessor :unit_price

validates_presence_of :unit_price, :message => "This is a custom validation message"

def before_validation
   self.unit_price = self.unit
end

Sameera

+1

Source: https://habr.com/ru/post/1761668/


All Articles