使用Rails我试图得到一个错误消息,如“歌曲字段不能为空”保存。做以下事情:
validates_presence_of :song_rep_xyz, :message => "can't be empty"
... 只显示“Song Rep XYW不能为空”,这是不好的,因为字段的标题不是用户友好的。如何更改字段本身的标题?我可以更改数据库中字段的实际名称,但我有多个“song”字段,我确实需要特定的字段名称。
我不想破坏rails的验证过程,我觉得应该有办法解决这个问题。
使用Rails我试图得到一个错误消息,如“歌曲字段不能为空”保存。做以下事情:
validates_presence_of :song_rep_xyz, :message => "can't be empty"
... 只显示“Song Rep XYW不能为空”,这是不好的,因为字段的标题不是用户友好的。如何更改字段本身的标题?我可以更改数据库中字段的实际名称,但我有多个“song”字段,我确实需要特定的字段名称。
我不想破坏rails的验证过程,我觉得应该有办法解决这个问题。
当前回答
是的,有一种方法可以在没有插件的情况下做到这一点! 但是它不像使用上面提到的插件那样干净和优雅。在这儿。
假设它是Rails 3(我不知道它在以前的版本中是否不同),
在你的模型中保持这个:
validates_presence_of :song_rep_xyz, :message => "can't be empty"
而在观,而不是离开
@instance.errors.full_messages
当我们使用脚手架生成器时,输入:
@instance.errors.first[1]
您将只得到您在模型中指定的消息,而没有属性名。
解释:
#returns an hash of messages, one element foreach field error, in this particular case would be just one element in the hash:
@instance.errors # => {:song_rep_xyz=>"can't be empty"}
#this returns the first element of the hash as an array like [:key,"value"]
@instance.errors.first # => [:song_rep_xyz, "can't be empty"]
#by doing the following, you are telling ruby to take just the second element of that array, which is the message.
@instance.errors.first[1]
到目前为止,我们只显示了一条消息,总是针对第一个错误。如果你想显示所有错误,你可以在哈希中循环并显示值。
希望这有帮助。
其他回答
如果你想把它们都列在一个漂亮的列表中,但又不想使用不友好的名字,你可以这样做……
object.errors.each do |attr,message|
puts "<li>"+message+"</li>"
end
现在,设置人性化名称和自定义错误消息的公认方法是使用区域设置。
# config/locales/en.yml
en:
activerecord:
attributes:
user:
email: "E-mail address"
errors:
models:
user:
attributes:
email:
blank: "is required"
现在已经更改了“email”属性的人性化名称和存在验证消息。
可以为特定的模型+属性、模型、属性或全局设置验证消息。
一个我从未见过任何人提到过的独特方法!
我能够获得我想要的所有定制的唯一方法是使用after_validation回调来允许我操纵错误消息。
允许正常创建验证消息,您不需要尝试在验证助手中更改它。 创建一个after_validation回调,它将在后端到达视图之前替换该验证消息。 在after_validation方法中,你可以对验证消息做任何你想做的事情,就像一个普通的字符串!您甚至可以使用动态值并将它们插入到验证消息中。
#this could be any validation
validates_presence_of :song_rep_xyz, :message => "whatever you want - who cares - we will replace you later"
after_validation :replace_validation_message
def replace_validation_message
custom_value = #any value you would like
errors.messages[:name_of_the_attribute] = ["^This is the replacement message where
you can now add your own dynamic values!!! #{custom_value}"]
end
after_validation方法的作用域将比内置的rails验证帮助器大得多,因此您将能够访问正在验证的对象,就像您尝试使用object.file_name一样。这在您试图调用它的验证助手中不起作用。
注意:我们在验证的开始使用^来去掉属性名,就像@Rystraum引用这个gem时指出的那样
在你的模型中:
validates_presence_of :address1, message: 'Put some address please'
在你看来
<% m.errors.each do |attr, msg| %>
<%= msg %>
<% end %>
如果你这样做
<%= attr %> <%= msg %>
您将得到带有属性名的错误消息
address1 Put some address please
如果您想获取单个属性的错误消息
<%= @model.errors[:address1] %>
是的,有一种方法可以在没有插件的情况下做到这一点! 但是它不像使用上面提到的插件那样干净和优雅。在这儿。
假设它是Rails 3(我不知道它在以前的版本中是否不同),
在你的模型中保持这个:
validates_presence_of :song_rep_xyz, :message => "can't be empty"
而在观,而不是离开
@instance.errors.full_messages
当我们使用脚手架生成器时,输入:
@instance.errors.first[1]
您将只得到您在模型中指定的消息,而没有属性名。
解释:
#returns an hash of messages, one element foreach field error, in this particular case would be just one element in the hash:
@instance.errors # => {:song_rep_xyz=>"can't be empty"}
#this returns the first element of the hash as an array like [:key,"value"]
@instance.errors.first # => [:song_rep_xyz, "can't be empty"]
#by doing the following, you are telling ruby to take just the second element of that array, which is the message.
@instance.errors.first[1]
到目前为止,我们只显示了一条消息,总是针对第一个错误。如果你想显示所有错误,你可以在哈希中循环并显示值。
希望这有帮助。