I would like to know if there is a way (a plugin?) to display all message errors for a given object in rails 3. For example, here my validati开发者_Python百科ons:
validates_presence_of :email, :message => "Your Email can't be blank"
validates_format_of :email, :with => /^[-a-z0-9_+\.]+\@([-a-z0-9]+\.)+[a-z0-9]{2,4}$/i
which will be displayed in the view like this:
Your Email can't be blank
Email is invalid
If I use error_message_on (@user, :email) I'll get the first message error, So my start has to create a loop and extract the related method (example the @user.email) Is it the right way?
EDIT
This almost achieves it:
<ul>
<%= @user.errors.on(:email).each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
But it returns also the array, How can I manage this?
Your Email can't be blank
is invalid
["Your Email can't be blank", "is invalid"]
Thank you!
<ul>
<% @user.errors.on(:email).each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
and as an helper:
def display_all_error_messages(object, method)
list_items = object.errors[method].map { |msg| content_tag(:li, msg) }
content_tag(:ul, list_items.join.html_safe)
end
精彩评论