How to display a Rails flash notice upon redirect?

at. picture at. · Mar 20, 2013 · Viewed 78.8k times · Source

I have the following code in a Rails controller:

flash.now[:notice] = 'Successfully checked in'
redirect_to check_in_path

Then in the /check_in view:

<p id="notice"><%= notice %></p>

However, the notice does not show up. Works perfect if I don't redirect in the controller:

flash.now[:notice] = 'Successfully checked in'
render action: 'check_in'

I need a redirect though... not just a rendering of that action. Can I have a flash notice after redirecting?

Answer

Rebitzele picture Rebitzele · Mar 20, 2013

Remove the .now. So just write:

flash[:notice] = 'Successfully checked in'
redirect_to check_in_path

The .now is specifically supposed to be used when you are just rendering and not redirecting. When redirecting, the .now is not to be used.