Form validation errors in Rails 3

I have a form I built using Formtastic in Rails 3. The form submits to the #create action of ClientsController, and if the save is successful the controller redirects to a "thank you" page. If the save is not successful, it redirects to the page where the form is embedded. I'd like to be able to show the validation errors inline on the form page, but after the redirect back to that page, the errors object comes back empty.

What can I do so I can display the validation errors on the form?

Here is the code from the controller.

if client.save && event.save
  redirect_to "/thank-you"
else
  redirect_to :back
end  

Some additional details: I am using RefineryCMS so there is no view for the page where the form is, and therefore I can't render the view.

Answers


You need to save your errors into session.

if client.save && event.save
  session[:client_create_errors] = nil
  redirect_to "/thank-you"
else
  session[:client_create_errors] = client.errors
  redirect_to :back
end

now you can access errors object from anywere using session[:client_create_errors]


Why not:

render :action => 'name-of-the-view-the-form-is-in'

instead of the redirect_to :back ?


Need Your Help

Bandwidth Data implications of embedding Youtube videos in my website

youtube video-streaming youtube-api

If I embed a Youtube video on my web page, what are the data usage implications on my server?

Objective-C: trimming whitespace

objective-c removing-whitespace

I have to be missing something. I'm trying to trim the data being pulled from an array but my app crashes on the trim method.