# problem with error\_messages\_for

**URL:** <https://discuss.rubyonrails.org/t/problem-with-error-messages-for/13415>\
**Category:** rubyonrails-talk\
**Created:** [May 24, 2007, 6:00pm UTC](https://discuss.rubyonrails.org/t/problem-with-error-messages-for/13415 "2007-05-24T18:00:55Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Craig\_White1](https://avatars.discourse-cdn.com/v4/letter/c/3ec8ea/32.png) [@Craig\_White1](https://discuss.rubyonrails.org/u/Craig_White1)\
**Post date:** [May 24, 2007, 6:00pm UTC](https://discuss.rubyonrails.org/t/problem-with-error-messages-for/13415/1 "2007-05-24T18:00:55Z")

</div>

I have a number of validations in client.rb like this...

&nbsp;&nbsp;validates\_length\_of :first\_name, &nbsp;&nbsp;&nbsp;&nbsp;:last\_name, &nbsp;&nbsp;&nbsp;&nbsp;:maximum =\> 25, &nbsp;&nbsp;&nbsp;&nbsp;:message =\> "Maximum 25 characters and numbers combined"

and in my clients\_controller.rb I have this...

&nbsp;&nbsp;def update &nbsp;&nbsp;&nbsp;&nbsp;@client = Client.find(params[:id]) &nbsp;&nbsp;&nbsp;&nbsp;if @client.update\_attributes(params[:client]) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;flash[:notice] = 'Client was successfully updated.' &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;redirect\_to :back &nbsp;&nbsp;&nbsp;&nbsp;else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;flash[:notice] = 'There was a problem with saving your edits.' &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;redirect\_to :back &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;end

and the first line in my view\_code is

&nbsp;&nbsp;\<%= error\_messages\_for 'client' %\>

my the flash[:notice] (on my layout) displays properly but nothing ever displays in the view code.

What detail am I missing? Is it because I am using ':redirect\_to :back' ?

---

<div class="post-metadata">

**Author:** ![Phrogz](https://avatars.discourse-cdn.com/v4/letter/p/2acd7d/32.png) [@Phrogz](https://discuss.rubyonrails.org/u/Phrogz)\
**Post date:** [May 24, 2007, 6:18pm UTC](https://discuss.rubyonrails.org/t/problem-with-error-messages-for/13415/2 "2007-05-24T18:18:27Z")

</div>

Yes - you need to use render instead to preserve the errors.

---

<div class="post-metadata">

**Author:** ![Craig\_White1](https://avatars.discourse-cdn.com/v4/letter/c/3ec8ea/32.png) [@Craig\_White1](https://discuss.rubyonrails.org/u/Craig_White1)\
**Post date:** [May 24, 2007, 6:51pm UTC](https://discuss.rubyonrails.org/t/problem-with-error-messages-for/13415/3 "2007-05-24T18:51:44Z")

</div>

> \> What detail am I missing? Is it because I am using \> ':redirect\_to :back' ?
> 
> Yes - you need to use render instead to preserve the errors.

---

<div class="post-metadata">

**Author:** ![Leon\_Bogaert](https://avatars.discourse-cdn.com/v4/letter/l/58f4c7/32.png) [@Leon\_Bogaert](https://discuss.rubyonrails.org/u/Leon_Bogaert)\
**Post date:** [May 30, 2007, 7:12pm UTC](https://discuss.rubyonrails.org/t/problem-with-error-messages-for/13415/4 "2007-05-30T19:12:11Z")

</div>

Does anybody has an answer for this? I have the same problem.

And adding a new user while the url is : users/save doesn't seem "REST" to me.

---

<div class="post-metadata">

**Author:** ![tfwright](https://avatars.discourse-cdn.com/v4/letter/t/f9ae1b/32.png) [@tfwright](https://discuss.rubyonrails.org/u/tfwright)\
**Post date:** [May 30, 2007, 9:12pm UTC](https://discuss.rubyonrails.org/t/problem-with-error-messages-for/13415/5 "2007-05-30T21:12:19Z")

</div>

If you need to access formatted errors in controllers your best bet is to use @object.errors.full\_messages, which returns an array of error messages corresponding to the LIs that error\_message\_for prints out. If you are constructing a flash message you might do something like

flash[:warning] = @object.errors.full\_messages.join(" ")

Which will yield something like "Attribute X can't be blank. Attribute Y must be numeric." etc

---

<div class="post-metadata">

**Author:** ![Leon\_Bogaert](https://avatars.discourse-cdn.com/v4/letter/l/58f4c7/32.png) [@Leon\_Bogaert](https://discuss.rubyonrails.org/u/Leon_Bogaert)\
**Post date:** [June 2, 2007, 2:25pm UTC](https://discuss.rubyonrails.org/t/problem-with-error-messages-for/13415/6 "2007-06-02T14:25:09Z")

</div>

Beautiful. Gonna try that. Thanks. But I think Rails won't remember the post values, right?
