简体   繁体   中英

Rails validation error messages, customizing the attribute name

例如,如果模型具有名为“unit”的属性,但在您的视图中,您将此属性称为“单价”,但是当您进行验证时,错误消息默认为“单位”,如何将其修改为“单价”?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM