Error_messages_for, can the css be turned off?

i looked in the api, but it seems the method doesnt take any parameters
except for the object. basically all i want is the string of errors but
without the form fields highlighted and such.


I don’t think there’s an easy answer to turning the CSS off (other
than just not defining that style in your stylesheet) but you can get
the text of the individual error messages with
“error_message_on(object, method)” like:

error_message_on(user, username)
=> already in use

error_message_on(post, title)
=> can not be blank

hi andrew, thanks for the answer, i now know how to use error on
message…however, i am still having a problem where when the form is
redisplayed, my fields are shifted due to the addition of divs…

thanks, i will keep looking.

Hi –

On 3/16/07, mioxplate [email protected] wrote:

hi andrew, thanks for the answer, i now know how to use error on
message…however, i am still having a problem where when the form is
redisplayed, my fields are shifted due to the addition of divs…

There’s a proc that generates that div wrapper, and you can override
it. You can see the original/default one right near the top of
helpers/active_record_helper.rb in the ActionView directory of the
Rails source. You can override it at the bottom of your
config/environment.rb file.

For example, here’s a simple replacement proc that will write an error
message in red right next to each offending field:

ActionView::Base.field_error_proc = {|html_tag, instance|



A bit too simple for real life, probably, but it shows you the idea.
You can find more info, and some nice pre-written replacement procs,
by googling for field_error_proc.


Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (
(See what readers are saying!
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (

might i recommend my gem
you can install it by doing:

gem install error_messages_for

because of the way it works you can pass in your own partial on each
request, use a default partial if it exists
(RAILS_ROOT/app/views/application/_error_messages.rhtml), or it will use
the default rails code. this allows you to fully customize your error
messages section.

it also offers other great things like the ability to handle multiple
objects with one error_messages_for call!

here’s the readme:


This gem enchances the default rails functionality for the ActiveRecord
helper ‘error_messages_for’

What it does:

  1. It allows you to enter an array of AR objects as the first argument
    and will display them all in the same output block.


<%= error_messages_for [:user, :topic, :item] %>

With this bit of code you’ll get something that looks like the

There were 3 errors that need to be fixed:

  • User username can’t be blank
  • Topic title can’t be blank
  • Item body can’t be blank
  1. The second argument allows you to pass in a partial to render the
    errors. By default the first thing it looks for is your partial, if
    that’s nil, it’ll look for the following partial:
    RAILS_ROOT/app/views/application/_error_messages.rhtml If it doesn’t
    find that it’ll default to some inline text.


<%= error_messages_for :user, “my_errors” %>

You had 2 errors fool!

  • User username can’t be blank
  • User email can’t be blank

This is cool if you want to change the way errors are displayed in a
pop-up vs. your main site, or you want to change them on a whole section
of the site, etc…

  1. A method is added to all of your AR objects called, ‘business_name’.
    By default this method will return the class of the AR object.


user.business_name returns “User”
item.business_name returns “Item”

This is used when generating the error messages. Here’s the way a
message is rendered: “business_name column_name error_message”. So the
following model:

class Item < ActiveRecord::Base
validates_presence_of :body, :message => “can’t be blank.”

would produce the following error message:

“Item body can’t be blank.”

The business_name method allows you to override the first part of the
error message if your business speak doesn’t match your database speak.
An example would be if your business wants to call Items, Articles. In
that case you would do the following:

class Item < ActiveRecord::Base
validates_presence_of :body, :message => “can’t be blank.”

def business_name

would produce the following error message:

“Article body can’t be blank.”

Pretty sweet, eh?

  1. The last thing you can do is override the entire message from start
    to finish. Let’s say, using the last example, that the business doesn’t
    like the message, “Article body can’t be blank.”, instead they want the
    message to be, “Oi, give us a body!!”. That’s easy. All you have to do
    is start your :message attribute with a “^”.


class Item < ActiveRecord::Base
validates_presence_of :body, :message => “^Oi, give us a body!!”

would produce the following error message:

“Oi, give us a body!!”

This gem offers you great flexibility, and probably should’ve been done
right from the start in Rails core. Hope it helps.

If you just replace that div [block level, and therefore layout
by default] with a layout-friendly span, it’s pretty much the same as
and can be styled the same but without the layout shifting and breaking

ActionView::Base.field_error_proc = do |html_tag, instance|
“<span class=“error”>#{html_tag}”

Also, because you can change the display of a span [or any element] to
“block”, this method is the simplest. Though, now that I think about
You could just change the display of the div to inline, couldn’t you?
Seems like there’s a few different ways to get this accomplished.