Forum: Ruby on Rails Ordering of messages in error_messages_for

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Bb4bdf2b184027bc38d4fb529770cde5?d=identicon&s=25 Wes Gamble (weyus)
on 2009-03-09 16:07
Is there any way to control the ordering of the error messages in the
output of error_messages_for()?

I want to make the error messages match the order of the form elements
in my form, and although the validates_presence_of directive has these
attributes in that order, they still show up in seemingly random order.

Any way to control this?

Thanks,
Wes
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-03-09 16:19
(Received via mailing list)
On 9 Mar 2009, at 15:07, Wes Gamble wrote:

>
> Is there any way to control the ordering of the error messages in the
> output of error_messages_for()?

At the end of the day the errors are stored in a hash and thus with no
guarenteed order. This might be a good time to replace
error_messages_for with code of your own (if you poke inside the
source for it you'll see that it's pretty simple).

Fred
Bb4bdf2b184027bc38d4fb529770cde5?d=identicon&s=25 Wes Gamble (weyus)
on 2009-03-09 16:32
Fred,

I already have a custom version of error_messages_for, so yeah, that's
cool.

Thx. for the quick response.

Wes
Aad37b5f7116c8d1f547d23b37566032?d=identicon&s=25 Greg Donald (destiney)
on 2009-03-09 18:08
(Received via mailing list)
On Mon, Mar 9, 2009 at 10:07 AM, Wes Gamble
<rails-mailing-list@andreas-s.net> wrote:
>
> Is there any way to control the ordering of the error messages in the
> output of error_messages_for()?
>
> I want to make the error messages match the order of the form elements
> in my form, and although the validates_presence_of directive has these
> attributes in that order, they still show up in seemingly random order.
>
> Any way to control this?


I put my error messages right under the fields they are for:

<%= f.username :text_field %>
<% if @user.errors['username'] -%>
<br /><span class="error"><%= @user.errors['username'] %></span>
<% end -%>

I did not like error_messages_for() when I first tried and I've since
found most of my clients don't care for it either.



--
Greg Donald
http://destiney.com/
4c438a80fc30661ad619ea177cf9cbd0?d=identicon&s=25 Freddy Andersen (Guest)
on 2009-03-26 18:36
(Received via mailing list)
You can do this:

<%= f.text_field :email, :label => "Email Address", :required => true
%>
<em><%= error_message_on @user, :email %></em>
This topic is locked and can not be replied to.