07
APR

When I first started using Rails I was puzzled why when submitting a form as an object, any field with errors where enclosed inside a <div> rather than an inline tag such as <span>. If you add <div>s around your field this may affect your website’s output by pushing anything next to your field onto a new line (outside the new DIV block).

Now, there are two ways you can resolve this issue.

1) You can add a CSS selector for #fieldWithErrors to set the display type to ‘inline’:

#fieldWithErrors { display:inline; }

2) As this is Rails, we can easily just change the markup which is output. We do this by modifying the ActionView::Base controller in our config/environment.rb file. Simply add the line below to this file to change the markup to use <span> tags insted of <div>.

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

Personally, I recommend the second option.

No comments yet... :(