Problem with error_messages_for

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. [email protected]

On May 24, 12:00 pm, Craig W. [email protected] 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.

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

On May 24, 12:00 pm, Craig W. [email protected] 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. [email protected]

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.

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

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