我正在努力了解如何实现这一目标。任何人都可以建议我或指出我正确的方向吗?
这允许每次显示每个字段1个错误。这几乎是我想做的,但并不完全正确。我想一次显示1个完整的错误消息。例如。名字不能为空。一旦解决了它就转移到下一个错误。因此,如果用户在他们的姓氏中添加了数字,它将不再是空白,但它会显示另一个错误,说明只允许字母等。当错误被修复时,它将转到姓氏错误或者如果用户字段可能发送电子邮件正确地说出他们的姓氏。
<% @user.errors.each do |attr, msg| %>
<%= "#{attr} #{msg}" if @user.errors[attr].first == msg %>
<% end %>
答案 0 :(得分:62)
ActiveRecord将验证错误存储在名为errors
的数组中。如果您有User
模型,那么您将在给定的实例中访问验证错误,如下所示:
@user = User.create[params[:user]] # create will automatically call validators
if @user.errors.any? # If there are errors, do something
# You can iterate through all messages by attribute type and validation message
# This will be something like:
# attribute = 'name'
# message = 'cannot be left blank'
@user.errors.each do |attribute, message|
# do stuff for each error
end
# Or if you prefer, you can get the full message in single string, like so:
# message = 'Name cannot be left blank'
@users.errors.full_messages.each do |message|
# do stuff for each error
end
# To get all errors associated with a single attribute, do the following:
if @user.errors.include?(:name)
name_errors = @user.errors[:name]
if name_errors.kind_of?(Array)
name_errors.each do |error|
# do stuff for each error on the name attribute
end
else
error = name_errors
# do stuff for the one error on the name attribute.
end
end
end
当然,如果您只想向用户显示第一个错误或其他内容,您也可以在视图中执行此操作而不是控制器。
答案 1 :(得分:28)
经过几个小时的实验,我发现了它。
<% if @user.errors.full_messages.any? %>
<% @user.errors.full_messages.each do |error_message| %>
<%= error_message if @user.errors.full_messages.first == error_message %> <br />
<% end %>
<% end %>
更好:
<%= @user.errors.full_messages.first if @user.errors.any? %>
答案 2 :(得分:7)
更好的主意,
如果您想将错误消息放在文本字段下方,可以这样做
A get-by-key operation (single or batch) for a cached entity will return the entity instance without a call to the datastore or even to the memcache
什么是.row.spacer20top
.col-sm-6.form-group
= f.label :first_name, "*Your First Name:"
= f.text_field :first_name, :required => true, class: "form-control"
= f.error_message_for(:first_name)
?
- &GT;嗯,这是一个很好的黑客做一些很酷的东西
error_message_for
标记错误后生成
# Author Shiva Bhusal
# Aug 2016
# in config/initializers/modify_rails_form_builder.rb
# This will add a new method in the `f` object available in Rails forms
class ActionView::Helpers::FormBuilder
def error_message_for(field_name)
if self.object.errors[field_name].present?
model_name = self.object.class.name.downcase
id_of_element = "error_#{model_name}_#{field_name}"
target_elem_id = "#{model_name}_#{field_name}"
class_name = 'signup-error alert alert-danger'
error_declaration_class = 'has-signup-error'
"<div id=\"#{id_of_element}\" for=\"#{target_elem_id}\" class=\"#{class_name}\">"\
"#{self.object.errors[field_name].join(', ')}"\
"</div>"\
"<!-- Later JavaScript to add class to the parent element -->"\
"<script>"\
"document.onreadystatechange = function(){"\
"$('##{id_of_element}').parent()"\
".addClass('#{error_declaration_class}');"\
"}"\
"</script>".html_safe
end
rescue
nil
end
end
对应的SCSS
<div id="error_user_email" for="user_email" class="signup-error alert alert-danger">has already been taken</div>
<script>document.onreadystatechange = function(){$('#error_user_email').parent().addClass('has-signup-error');}</script>
注意:此处使用的引导变量
答案 3 :(得分:2)
我这样解决了:
<% @user.errors.each do |attr, msg| %>
<li>
<%= @user.errors.full_messages_for(attr).first if @user.errors[attr].first == msg %>
</li>
<% end %>
这样您就可以使用区域设置来显示错误消息。