3

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

fivetwentysix
  • 7,379
  • 9
  • 40
  • 58

2 Answers2

2

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

en:
  activerecord:
    attributes:
      product:
        unit:
          one:   Unit price
          other: Unit prices
graywh
  • 9,640
  • 2
  • 29
  • 27
Andrew Vit
  • 18,961
  • 6
  • 77
  • 84
  • Doesn't seem to work for me. I haven't worked on localization yet, is there anything I need to do before this would work? – fivetwentysix Aug 26 '10 at 07:23
  • 1
    Fixed my answer: I had answered for the model name, not the attribute name. – Andrew Vit Aug 26 '10 at 07:50
  • 1
    @Andrew Vit It is wrong answer. Before the line with the name of the attribute, you need one line with the name of the model. See for example how the same problem is answered here: http://stackoverflow.com/questions/808547/fully-custom-validation-error-message-with-rails – p.matsinopoulos Feb 18 '12 at 15:01
1

I'm not sure how you can change the column name , But following is a working workaround

in your model create a virtual attribute called unit_price

something like this

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

cheers

sameera

sameera207
  • 16,547
  • 19
  • 87
  • 152