Fixing div.fieldWithErrors in Ruby on Rails

Saturday, May 5th, 2007

Rails’ form helpers are incredibly handy in quickly writing form code, however their error handling code leaves something to be desired. By default, when a field has errors associated with it, the input is wrapped in a div of class “fieldWithErrors”. Unfortunately, this behavior requires you to explicitly change the div to display inline so it doesn’t make the input appear on the next line after the name of the field. I have also had some trouble getting this setting to work in Safari, although that may just be my problem and not Safari’s.

Fortunately, fixing this annoying little gotcha is as easy as adding a single line to the end of your environment.rb file: (I’ve split it into two lines so it will fit on the page, just remove the line break)

ActionView::Base.field_error_proc = Proc.new { |html_tag, instance|
"<span class=\"fieldWithErrors\">#{html_tag}</span>" }

I know many people have already written about this problem, but I wanted to have my own record of it for easy reference ;)

4 Responses »

  1. Vinay - May 29th, 2007 at 10:36 pm

    Hey thanks for that man it was really helpful and it worked great. Great job. Keep up the good work.

  2. Vinay - June 1st, 2007 at 2:58 am

    Hey while doing this the “” is replaced by “” any suggestions of getting rid of it? Thanks in advance.:)

  3. Connor - July 7th, 2007 at 3:28 pm

    I’m afraid I’m not really sure what you’re referring to Vinay…

  4. carlivar - August 9th, 2007 at 4:14 pm

    http://dev.rubyonrails.org/ticket/3587

Leave a Reply

Comments will be styled using Markdown.