# Error\_messages\_for question

**URL:** <https://www.ruby-forum.com/t/error-messages-for-question/151274>\
**Category:** Rails\
**Created:** [October 19, 2008, 1:36pm UTC](https://www.ruby-forum.com/t/error-messages-for-question/151274 "2008-10-19T13:36:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jicksta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7ba0ec/32.png) [@jicksta](https://www.ruby-forum.com/u/jicksta)\
**Post date:** [October 19, 2008, 1:36pm UTC](https://www.ruby-forum.com/t/error-messages-for-question/151274/1 "2008-10-19T13:36:44Z")

</div>

Hi I got a question about error\_messages\_for helper method in rails. I  
read that keeping it in the view helps report validation failure. I have  
used as follows in checkout.rhtml:

checkout.rhtml:

\<%=error\_messages\_for 'customer'%\> \<%form\_for :customer .....%\> ............ \<%end%\>

customer.rb:  
…  
validates\_uniqueness\_of :name  
…

Now, if I try to give the same name twice, it should display its default  
message “has already been taken” but mine doesn’t just the checkout page  
is redisplayed. Any suggestions…

thanks.

---

<div class="post-metadata">

**Author:** ![jicksta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7ba0ec/32.png) [@jicksta](https://www.ruby-forum.com/u/jicksta)\
**Post date:** [October 19, 2008, 4:14pm UTC](https://www.ruby-forum.com/t/error-messages-for-question/151274/2 "2008-10-19T16:14:20Z")

</div>

Instead of

\<%= error\_messages\_for ‘customer’ %\>

which is telling error\_messages\_for to display errors for the string  
‘customer’, which I don’t expect to ever work, you should probably use

\<%= error\_messages\_for @customer %\>

assuming that your controller action sets @customer for use by the view.  
If  
that’s not the case, could you show us the code for your action?

Regards,  
Craig

---

<div class="post-metadata">

**Author:** ![jicksta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7ba0ec/32.png) [@jicksta](https://www.ruby-forum.com/u/jicksta)\
**Post date:** [October 20, 2008, 7:14am UTC](https://www.ruby-forum.com/t/error-messages-for-question/151274/3 "2008-10-20T07:14:04Z")

</div>

Craig D. wrote:

> Instead of
> 
> \<%= error\_messages\_for ‘customer’ %\>
> 
> which is telling error\_messages\_for to display errors for the string  
> ‘customer’, which I don’t expect to ever work, you should probably use
> 
> \<%= error\_messages\_for @customer %\>
> 
> assuming that your controller action sets @customer for use by the view.  
> If  
> that’s not the case, could you show us the code for your action?
> 
> Regards,  
> Craig

Thank Craig for the reply. I did as you said but ended up getting error  
“@\<Customer not allowed” something like this, so I instead did the other  
way:  
def checkout  
customer = Customer.new  
end

and then: \<%=error\_messages\_for ‘customer’%\>

it worked but I get ugly underline in customer = Customer.new saying  
that customer hasn’t been used. Also I have made .css file for the  
flash[:notice] but its not getting applied to the generated error  
message while it does to other error msgs. By the way the above  
mentioned checkout action is exactly as I’ve put in the controller and  
in the view as I’ve mentioned in my 1st post. Thanks.

---

<div class="post-metadata">

**Author:** ![jicksta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7ba0ec/32.png) [@jicksta](https://www.ruby-forum.com/u/jicksta)\
**Post date:** [October 20, 2008, 2:04pm UTC](https://www.ruby-forum.com/t/error-messages-for-question/151274/4 "2008-10-20T14:04:46Z")

</div>

error\_messages\_for ‘customer’

by convention looks for @customer. In your controller action, you need  
to do

def checkout  
@customer = Customer.new  
end

Once you have error\_messages\_for working, you can view the source of  
your  
page to see that error\_messages\_for generates specific markup for the  
errors.

## 1 error prohibited this billing plan from being saved

There were problems with the following fields:

- Name can't be blank

(Of course, the text in your error message(s) will be different.)  
Knowing  
the markup, you should be able to style it as you like.

Regards,  
Craig
