# Problem with error\_messages\_for

**URL:** <https://www.ruby-forum.com/t/problem-with-error-messages-for/105000>\
**Category:** Rails\
**Created:** [May 24, 2007, 8:01pm UTC](https://www.ruby-forum.com/t/problem-with-error-messages-for/105000 "2007-05-24T20:01:51Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [May 24, 2007, 8:01pm UTC](https://www.ruby-forum.com/t/problem-with-error-messages-for/105000/1 "2007-05-24T20:01:51Z")

</div>

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

validates\_length\_of :first\_name,  
:last\_name,  
:maximum =\> 25,  
:message =\> “Maximum 25 characters and numbers combined”

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

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

and the first line in my view\_code is

\<%= 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’ ?

–  
Craig W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [May 24, 2007, 8:19pm UTC](https://www.ruby-forum.com/t/problem-with-error-messages-for/105000/2 "2007-05-24T20:19:55Z")

</div>

On May 24, 12:00 pm, Craig W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [May 24, 2007, 8:53pm UTC](https://www.ruby-forum.com/t/problem-with-error-messages-for/105000/3 "2007-05-24T20:53:01Z")

</div>

On Thu, 2007-05-24 at 11:18 -0700, Phrogz wrote:

> On May 24, 12:00 pm, Craig W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > 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.

* * *

OK - that’s why they stopped outputting the ‘messages’

I really want to use ‘redirect\_to’ instead of ‘render’ because I have a  
number of different data entry screens using the same ‘update’ method.

Is it possible to get the same effect in flash[:notice] - I ask because  
‘error\_messages\_for’ is ActionView and doesn’t work in controllers and  
something like

flash[:notice] = error\_messages\_for ‘client’  
flash[:notice] = @client.errors  
flash[:notice] = @client.valid?

all don’t work

–  
Craig W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [May 30, 2007, 9:13pm UTC](https://www.ruby-forum.com/t/problem-with-error-messages-for/105000/4 "2007-05-30T21:13:14Z")

</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:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [May 30, 2007, 11:13pm UTC](https://www.ruby-forum.com/t/problem-with-error-messages-for/105000/5 "2007-05-30T23:13:39Z")

</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:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [June 2, 2007, 4:25pm UTC](https://www.ruby-forum.com/t/problem-with-error-messages-for/105000/6 "2007-06-02T16:25:51Z")

</div>

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