views:

258

answers:

3

Is there any (simple) way to get some control of the order in which a model's errors appear in the view? Ordering the rules does not seem to help whatsoever.

+2  A: 

Use error_message_on instead of error_messages to get the message for an individual attribute.

<div class="errorMessages">
    <% %{name title description}.each do |att| %>
        <%= f.error_message_on att, :css_class => "error" %>
    <% end %>
</div>
Baldu
Perfect. That's what I needed.
Yar
A: 

Here's an answer (for my own notes, basically) using Baldu's answer. This puts the attribute_names in alpha order:

<% if @model.errors.length>0 %>
  <div class="errorExplanation">
    <h3>There were problems with the following fields:</h3><ul>
      <% @model.attribute_names.each do |attribute| %>
        <% if [email protected][attribute].blank? %>
          <li><%= f.error_message_on attribute, Model.human_attribute_name(attribute)+ "&nbsp;", :style=>"display:inline" %></li>
        <% end %>
      </ul>
    <% end %>
  </div>
<% end %>

Of course you can parametrize this further as a partial, for instance. I'll probably do that :)

Yar
A: 

In 2.3.6 validation messages will show in order you declared them in code

link

Adrian Serafin