使用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的验证过程,我觉得应该有办法解决这个问题。
当前回答
这里有另一种方法:
如果你使用这个模板:
<% if @thing.errors.any? %>
<ul>
<% @thing.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
<% end %>
你可以像这样写你自己的自定义消息:
class Thing < ActiveRecord::Base
validate :custom_validation_method_with_message
def custom_validation_method_with_message
if some_model_attribute.blank?
errors.add(:_, "My custom message")
end
end
这样,由于下划线的存在,整个消息变成了“我的自定义消息”,但开头的额外空间是不明显的。如果你真的不想在开始的时候增加额外的空间,只需添加.lstrip方法。
<% if @thing.errors.any? %>
<ul>
<% @thing.errors.full_messages.each do |message| %>
<li><%= message.lstrip %></li>
<% end %>
</ul>
<% end %>
的字符串。Lstrip方法将删除':_'所创建的额外空格,并将保持任何其他错误消息不变。
或者更好的是,使用自定义消息的第一个单词作为键:
def custom_validation_method_with_message
if some_model_attribute.blank?
errors.add(:my, "custom message")
end
end
现在完整的消息将是“我的自定义消息”,没有额外的空间。
如果你想要完整的信息以一个大写的词开头,比如“URL不能为空”,这是不可能的。相反,试着添加一些其他的单词作为关键字:
def custom_validation_method_with_message
if some_model_attribute.blank?
errors.add(:the, "URL can't be blank")
end
end
现在完整的信息是" URL不能为空"
其他回答
是的,有一种方法可以在没有插件的情况下做到这一点! 但是它不像使用上面提到的插件那样干净和优雅。在这儿。
假设它是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]
到目前为止,我们只显示了一条消息,总是针对第一个错误。如果你想显示所有错误,你可以在哈希中循环并显示值。
希望这有帮助。
在自定义验证方法中使用:
错误。add(:base, "自定义错误消息")
因为add_to_base已弃用。
错误。add_to_base("自定义错误消息")
我试着跟随,为我工作:)
1 job.rb
class Job < ApplicationRecord
validates :description, presence: true
validates :title,
:presence => true,
:length => { :minimum => 5, :message => "Must be at least 5 characters"}
end
2 jobs_controller.rb
def create
@job = Job.create(job_params)
if @job.valid?
redirect_to jobs_path
else
render new_job_path
end
end
3 _form.html.erb
<%= form_for @job do |f| %>
<% if @job.errors.any? %>
<h2>Errors</h2>
<ul>
<% @job.errors.full_messages.each do |message|%>
<li><%= message %></li>
<% end %>
</ul>
<% end %>
<div>
<%= f.label :title %>
<%= f.text_field :title %>
</div>
<div>
<%= f.label :description %>
<%= f.text_area :description, size: '60x6' %>
</div>
<div>
<%= f.submit %>
</div>
<% end %>
我建议安装David Easley最初编写的custom_error_message gem(或作为插件)
它可以让你做以下事情:
validates_presence_of :non_friendly_field_name, :message => "^Friendly field name is blank"
这里有另一种方法:
如果你使用这个模板:
<% if @thing.errors.any? %>
<ul>
<% @thing.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
<% end %>
你可以像这样写你自己的自定义消息:
class Thing < ActiveRecord::Base
validate :custom_validation_method_with_message
def custom_validation_method_with_message
if some_model_attribute.blank?
errors.add(:_, "My custom message")
end
end
这样,由于下划线的存在,整个消息变成了“我的自定义消息”,但开头的额外空间是不明显的。如果你真的不想在开始的时候增加额外的空间,只需添加.lstrip方法。
<% if @thing.errors.any? %>
<ul>
<% @thing.errors.full_messages.each do |message| %>
<li><%= message.lstrip %></li>
<% end %>
</ul>
<% end %>
的字符串。Lstrip方法将删除':_'所创建的额外空格,并将保持任何其他错误消息不变。
或者更好的是,使用自定义消息的第一个单词作为键:
def custom_validation_method_with_message
if some_model_attribute.blank?
errors.add(:my, "custom message")
end
end
现在完整的消息将是“我的自定义消息”,没有额外的空间。
如果你想要完整的信息以一个大写的词开头,比如“URL不能为空”,这是不可能的。相反,试着添加一些其他的单词作为关键字:
def custom_validation_method_with_message
if some_model_attribute.blank?
errors.add(:the, "URL can't be blank")
end
end
现在完整的信息是" URL不能为空"