# Error\_messages\_for, can the css be turned off?

**URL:** <https://www.ruby-forum.com/t/error-messages-for-can-the-css-be-turned-off/89081>\
**Category:** Rails\
**Created:** [March 17, 2007, 1:12am UTC](https://www.ruby-forum.com/t/error-messages-for-can-the-css-be-turned-off/89081 "2007-03-17T01:12:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mixplate](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@mixplate](https://www.ruby-forum.com/u/mixplate)\
**Post date:** [March 17, 2007, 1:12am UTC](https://www.ruby-forum.com/t/error-messages-for-can-the-css-be-turned-off/89081/1 "2007-03-17T01:12:14Z")

</div>

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.

thanks.

---

<div class="post-metadata">

**Author:** ![mixplate](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@mixplate](https://www.ruby-forum.com/u/mixplate)\
**Post date:** [March 17, 2007, 1:36am UTC](https://www.ruby-forum.com/t/error-messages-for-can-the-css-be-turned-off/89081/2 "2007-03-17T01:36:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mixplate](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@mixplate](https://www.ruby-forum.com/u/mixplate)\
**Post date:** [March 17, 2007, 1:51am UTC](https://www.ruby-forum.com/t/error-messages-for-can-the-css-be-turned-off/89081/3 "2007-03-17T01:51:49Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mixplate](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@mixplate](https://www.ruby-forum.com/u/mixplate)\
**Post date:** [March 17, 2007, 4:40am UTC](https://www.ruby-forum.com/t/error-messages-for-can-the-css-be-turned-off/89081/4 "2007-03-17T04:40:23Z")

</div>

Hi –

On 3/16/07, mioxplate [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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 = Proc.new {|html\_tag, instance|  
“

#{instance.method\_name.capitalize}  
#{instance.error\_message}

#{html\_tag}”}

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.

David

–  
Q. What is THE Ruby book for Rails developers?  
A. RUBY FOR RAILS by David A. Black ([http://www.manning.com/black](http://www.manning.com/black))  
(See what readers are saying! [http://www.rubypal.com/r4rrevs.pdf](http://www.rubypal.com/r4rrevs.pdf))  
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?  
A. Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))

---

<div class="post-metadata">

**Author:** ![mixplate](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@mixplate](https://www.ruby-forum.com/u/mixplate)\
**Post date:** [March 17, 2007, 3:11am UTC](https://www.ruby-forum.com/t/error-messages-for-can-the-css-be-turned-off/89081/5 "2007-03-17T03:11:39Z")

</div>

might i recommend my gem [http://rubyforge.org/projects/railserrorsfor/](http://rubyforge.org/projects/railserrorsfor/)  
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:

# ErrorMessagesFor

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.

Example:

\<%= error\_messages\_for [:user, :topic, :item] %\>

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

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.

Example:

\<%= 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.

Example:

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.”  
end

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  
“Article”  
end  
end

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 “^”.

Example:

class Item \< ActiveRecord::Base  
validates\_presence\_of :body, :message =\> “^Oi, give us a body!!”  
end

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.

---

<div class="post-metadata">

**Author:** ![mixplate](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@mixplate](https://www.ruby-forum.com/u/mixplate)\
**Post date:** [March 18, 2007, 4:05pm UTC](https://www.ruby-forum.com/t/error-messages-for-can-the-css-be-turned-off/89081/6 "2007-03-18T16:05:33Z")

</div>

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

ActionView::Base.field\_error\_proc = Proc.new do |html\_tag, instance|  
“\<span class=“error”\>#{html\_tag}”  
end

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  
it…  
You could just change the display of the div to inline, couldn’t you?  
Hrm…  
Seems like there’s a few different ways to get this accomplished.

RSL
