23
我想知道是否有方法来更改与其关联的验证错误的字段名称。例如,如果我没有任何数据提交名字(表中确实为fname
),它就会大喊Fname can't be blank.
在Rails中更改错误字段名称
是否可以将其更改为First Name can't be blank
?
我想知道是否有方法来更改与其关联的验证错误的字段名称。例如,如果我没有任何数据提交名字(表中确实为fname
),它就会大喊Fname can't be blank.
在Rails中更改错误字段名称
是否可以将其更改为First Name can't be blank
?
一般的做法现在给天是编辑locals像这样:
# config/locales/en.yml
en:
activerecord:
attributes:
user:
fname: "First Name"
你的错误消息现在会说:“首先名称不能......”
为了完整为了你,你有另一种选择。将以下内容添加到用户模型中:
class User < ActiveRecord::Base
HUMANIZED_ATTRIBUTES = {
:fname => "First Name"
}
def self.human_attribute_name(attr)
HUMANIZED_ATTRIBUTES[attr.to_sym] || super
end
end
+1。但我认为'def self.human_attribute_name(attr)'不是必需的。 – Swanand 2011-04-26 05:36:22
in Rails 4我必须为'self.human_attribute_name(attr)'方法调用添加第二个参数。所以它变成'self.human_attribute_name(attr,options = {})' – Peter 2013-07-05 14:48:17