ruby-on-railsruby-on-rails-3ruby-on-rails-3.2ruby-on-rails-4rails-flash

How to display a Rails flash notice upon redirect?


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?


Solution

  • 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.