views:

20

answers:

2

Is there a way to access form errors in the controller? In the view I can access form errors using

from.error_messages

I want to list errors in a flash message before may layout header, how do I go about accessing those errors in the controller so I can assign them to a flash message?

+1  A: 
@model_instance.errors.full_messages.to_sentence
Jed Schneider
This seems close but it gives me: "Name can't be blank and City can't be blank" I'm looking for the full error message. When I display from the from I get "2 errors prohibited this location from being savedThere were problems with the following fields:Name can't be blankCity can't be blank"
Nick Faraday
that is the full message on associated attributes. Have a look at my discussion here: http://jedschneider.posterous.com/validation-messages-on-associated-attributes
Jed Schneider
This works... I just built a application helper method to format the array the same way the form helpers do. Seems a bit redundant as the form helper code already does this but this will work.
Nick Faraday
would love to see your solution, since with my solution, i wasnt very happy about putting this code in the parent model.
Jed Schneider
Can't really post code as a comment but here's the gist of it. I used your solution above to assign the error array to flash[:errors] in the controller. Then in my view I look to see if flash[:errors] is nil if not I display error_format(flash[:errors]). error_format is a helper method I wrote in application_helper that formats the error message with # errors occurred then an ol list. I use css to style from there. Hope that helps.
Nick Faraday
A: 

You may find error_messages_for and similar methods to be more convenient way to display errors in another place. More details here:
http://guides.rubyonrails.org/activerecord_validations_callbacks.html#displaying-validation-errors-in-the-view

What you want well may be already supported, as it's often the case with rails.

Nikita Rybak