# Ordering of messages in error\_messages\_for

**URL:** <https://www.ruby-forum.com/t/ordering-of-messages-in-error-messages-for/162398>\
**Category:** Rails\
**Created:** [March 9, 2009, 4:07pm UTC](https://www.ruby-forum.com/t/ordering-of-messages-in-error-messages-for/162398 "2009-03-09T16:07:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [March 9, 2009, 4:07pm UTC](https://www.ruby-forum.com/t/ordering-of-messages-in-error-messages-for/162398/1 "2009-03-09T16:07:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [March 9, 2009, 4:19pm UTC](https://www.ruby-forum.com/t/ordering-of-messages-in-error-messages-for/162398/2 "2009-03-09T16:19:47Z")

</div>

On 9 Mar 2009, at 15:07, Wes G. 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

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [March 9, 2009, 4:32pm UTC](https://www.ruby-forum.com/t/ordering-of-messages-in-error-messages-for/162398/3 "2009-03-09T16:32:18Z")

</div>

Fred,

I already have a custom version of error\_messages\_for, so yeah, that’s  
cool.

Thx. for the quick response.

Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [March 9, 2009, 6:08pm UTC](https://www.ruby-forum.com/t/ordering-of-messages-in-error-messages-for/162398/4 "2009-03-09T18:08:42Z")

</div>

On Mon, Mar 9, 2009 at 10:07 AM, Wes G.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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’] -%\>  
  
\<%= @user.errors[‘username’] %\>  
\<% 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 D.  
[http://destiney.com/](http://destiney.com/)

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [March 26, 2009, 6:36pm UTC](https://www.ruby-forum.com/t/ordering-of-messages-in-error-messages-for/162398/5 "2009-03-26T18:36:24Z")

</div>

You can do this:

\<%= f.text\_field :email, :label =\> “Email Address”, :required =\> true  
%\>  
_\<%= error\_message\_on @user, :email %\>_
