简体   繁体   English

Rails验证错误消息,自定义属性名称

[英]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 在您的模型中创建一个名为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 sameera

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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