问题描述
我正在尝试了解如何实现这一目标.谁能给我建议或指出正确的方向?
I'm trying to understand how I can achieve this. Can anyone advise me or point me in the right direction?
我目前所做的(如下面的代码片段所示)允许我一次显示每个字段中的 1 个错误.这不完全是我想要做的.
What I currently do, which is shown in the code snippet below, allows me to display 1 error from each field at a time. It’s not not quite exactly what I want to do.
我想一次显示 1 条错误消息.例如,名字不能为空",那么一旦解决了该错误,就会显示错误数组中的下一个错误.这应该会一直发生,直到所有错误都得到解决.
I want to display 1 error message at a time. For example, "first name can't be blank", then once that error has been resolved, the next error in the array of errors should be displayed. This should keep happening until all errors have been resolved.
<% @user.errors.each do |attr, msg| %>
<%= "#{attr} #{msg}" if @user.errors[attr].first == msg %>
<% end %>
推荐答案
经过几个小时的实验,我想通了.
After experimenting for a few hours I figured it out.
<% 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? %>
这篇关于如何一次显示一条 Ruby on Rails 表单验证错误消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!