开发者

Rails 3 - Display all message errors for a given method?

开发者 https://www.devze.com 2023-02-14 09:00 出处:网络
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:

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
0

精彩评论

暂无评论...
验证码 换一张
取 消