# Error\_messages\_for doesn't work

**URL:** <https://www.ruby-forum.com/t/error-messages-for-doesnt-work/193481>\
**Category:** Rails\
**Created:** [August 18, 2010, 2:02pm UTC](https://www.ruby-forum.com/t/error-messages-for-doesnt-work/193481 "2010-08-18T14:02:16Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![upabibi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/e19adc/32.png) [@upabibi](https://www.ruby-forum.com/u/upabibi)\
**Post date:** [August 18, 2010, 2:02pm UTC](https://www.ruby-forum.com/t/error-messages-for-doesnt-work/193481/1 "2010-08-18T14:02:16Z")

</div>

I’m having a problem, 'cause I wrote the following line in my model  
“Hi”:

```auto
validates_numericality_of :phone, :only_integer => true, :allow_blank =>
false, :message => "must be a number"
```

And in my “new” view, I put:

```auto
 <%= error_messages_for :oi,
                    :header_message => "Erro ao cadastrar usuÃ¡rio." ,
                    :message => "Cheque o(s) seguinte(s) campo(s):"%>
```

Rails notice the error, because I can’t save a Hi object with phone not  
being a number; but it doesn’t show me the errors that was find.

What it should be?

Thanks for any help.

---

<div class="post-metadata">

**Author:** ![upabibi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/e19adc/32.png) [@upabibi](https://www.ruby-forum.com/u/upabibi)\
**Post date:** [August 18, 2010, 2:50pm UTC](https://www.ruby-forum.com/t/error-messages-for-doesnt-work/193481/2 "2010-08-18T14:50:32Z")

</div>

On 18 August 2010 13:02, Bla … [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> [code]  
> \<%= error\_messages\_for :oi,

I think that should be hi not oi, and I think you have to put the  
model name in quotes also, so  
error\_messages\_for ‘hi’, …

Colin

---

<div class="post-metadata">

**Author:** ![upabibi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/e19adc/32.png) [@upabibi](https://www.ruby-forum.com/u/upabibi)\
**Post date:** [August 18, 2010, 3:19pm UTC](https://www.ruby-forum.com/t/error-messages-for-doesnt-work/193481/3 "2010-08-18T15:19:14Z")

</div>

Yeah, I’m sorry.

Actually, I wrote it wrong here, in the forum.

It’s:

```auto
 <%= error_messages_for :hi,
                    :header_message => "Error." ,
                    :message => "Check the following camps:"%>
```

---

<div class="post-metadata">

**Author:** ![upabibi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/e19adc/32.png) [@upabibi](https://www.ruby-forum.com/u/upabibi)\
**Post date:** [August 18, 2010, 4:31pm UTC](https://www.ruby-forum.com/t/error-messages-for-doesnt-work/193481/4 "2010-08-18T16:31:50Z")

</div>

On Aug 18, 2:19 pm, “Bla …” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> [/code]

What’s in your controller ?

Fred

---

<div class="post-metadata">

**Author:** ![upabibi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/e19adc/32.png) [@upabibi](https://www.ruby-forum.com/u/upabibi)\
**Post date:** [August 18, 2010, 4:34pm UTC](https://www.ruby-forum.com/t/error-messages-for-doesnt-work/193481/5 "2010-08-18T16:34:57Z")

</div>

this tag “`" and "`” is just to let everyone know where  
begins and where ends my code.  
It’s not in my controller…

---

<div class="post-metadata">

**Author:** ![upabibi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/e19adc/32.png) [@upabibi](https://www.ruby-forum.com/u/upabibi)\
**Post date:** [August 19, 2010, 1:04pm UTC](https://www.ruby-forum.com/t/error-messages-for-doesnt-work/193481/6 "2010-08-19T13:04:01Z")

</div>

I try to put in quotes, but it also doesn’t works…

---

<div class="post-metadata">

**Author:** ![upabibi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/e19adc/32.png) [@upabibi](https://www.ruby-forum.com/u/upabibi)\
**Post date:** [August 18, 2010, 4:57pm UTC](https://www.ruby-forum.com/t/error-messages-for-doesnt-work/193481/7 "2010-08-18T16:57:01Z")

</div>

On 18 August 2010 14:19, Bla … [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Yeah, I’m sorry.
> 
> Actually, I wrote it wrong here, in the forum.
> 
> It’s:
> 
> [code]  
> \<%= error\_messages\_for :hi,  
> :header\_message =\> “Error.” ,  
> :message =\> “Check the following camps:”%\>

The docs at  
[http://api.rubyonrails.org/classes/ActionView/Helpers/ActiveRecordHelper.html#M002221](http://api.rubyonrails.org/classes/ActionView/Helpers/ActiveRecordHelper.html#M002221)  
show the name in quotes, as I suggested in my previous post. So  
error\_messages\_for ‘hi’, …  
have you tried that?

Colin

---

<div class="post-metadata">

**Author:** ![upabibi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/e19adc/32.png) [@upabibi](https://www.ruby-forum.com/u/upabibi)\
**Post date:** [August 19, 2010, 3:41pm UTC](https://www.ruby-forum.com/t/error-messages-for-doesnt-work/193481/8 "2010-08-19T15:41:15Z")

</div>

Bla … wrote:

> I try to put in quotes, but it also doesn’t works…

Can you paste the content that it renders? Also, does anything scroll by  
in your server window when you try this?

Additionally, wanted to mention that error\_messages\_for was removed from  
Rails 3, though available as a plugin. So maybe consider just going with  
a custom solution now. Here is an snippet that I’ve used, though note it  
is in HAML, not ERB.

# views/shared/\_error\_messages.html.haml

- if target.errors.any?  
#errorExplanation  
%h2= “#{pluralize(target.errors.count, ‘error’)} prohibited this  
record from being saved:”  
%ul  
- target.errors.full\_messages.each do |message|  
%li= message

Then used with

# views/thingy/\_form.html.haml

#errors= render ‘shared/error\_messages’, :target =\> @company

You get a bit more control over what is going on here and are protected  
against the imminent removal of the helper.
