Topic: How to display validation messages after redirection?

Hi,

In my controller code if my model does not save correctly, I redirect to another controller. How do I show the model.errors validation messages in the redirected page?


Currently I have a workaround like this:

if @comment.save

   ...

else
   @comment.errors.each
   { |key, message|
     flash[:notice]=flash[:notice] + message
   }
   redirect_to ....
end


But I feel that there might be a better solution that I'm missing, would appreciate it if anyone could point it out, thanks a lot!

Last edited by oinvertedworld (2006-12-26 05:46:40)

Re: How to display validation messages after redirection?

The custom at the moment is to do something like this:

if @comment.save
  redirect_to comment_path(@comment)
  # or, if you're not using map.resources:
  # redirect_to :action => 'show', :id => @comment.id
else
  render :action => 'edit'
end

Since the model errors can't make it through a page redirect it's customary to just render the form.

If you *need* to redirect you could put the model in the session and set up a before_filter that catches the model from the session so the errors are preserved.