Domanda

I want to just show the errors for the attributes, not that a certain object is not valid in the view. How could I handle this?

I currently use this:

<% if !@ozb_konto.errors.nil? && @ozb_konto.errors.any? %>
<div class="alert alert-error" id="error_explanation">
  <h3>Following errors occured:</h3>    
  <ul>
    <% @ozb_konto.errors.full_messages.each do |error| %>
      <li><%= error %></li>
    <% end %>
      </ul>
</div>
<% end %>

This displays all kind of errors, which is correct. But it also displays the <model> is not valid errors which I want to supress.

How can I handle this?

Thanks in advance!

È stato utile?

Soluzione

As full_messages method is:

def full_messages
  map { |attribute, message| full_message(attribute, message) }
end

you can convert errors to hash and output it as you want:

@ozb_konto.errors.to_hash # => {:email=>["can't be blank"], :password=>["can't be blank"], :name=>["can't be blank"]} e.g
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top