I am occasionally faced with the sometimes awkward problem of how to store a Flash message during more than one redirect. The problem is that the flash is usually cleared after one redirect, but sometimes you want to redirect more than once, but retain some flash message to display on the final destination.
These less than well known Rails methods probably hold the answer.
http://api.rubyonrails.com/classes/ActionController/Flash/FlashHash.html







2 Comments
I’m having trouble figuring out (from what you’ve written) whether or not you’ve solved this problem for yourself, so please ignore this if you have.
I ran into the exact same issue today, and after following the link from your post above, I found the solution in the form of the “keep” method.
In my case, I have three actions; a, b & c, where a redirects to b with a flash message, and b redirects to c. By default, the flash message is lost by the time c is rendered.
To get around the problem, I changed action ‘b’ as follows:
def b
…
flash.keep
redirect_to :action => :b
end
Now when c is rendered, the message set in a is present.
Thanks anyway for the link!
No problem. I worked it out but didn’t think it needed a full write-up… it probably is useful though, so cheers.
Very useful methods anyway… I’d hacked some really quite unpleasant solutions to that little problem before I discovered them.