Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 408
at. Avatar asked Mar 20 '13 20:03

at.


People also ask

What is the difference between render and redirect in Rails?

Render tells Rails which view or asset to show a user, without losing access to any variables defined in the controller action. Redirect is different. The redirect_to method tells your browser to send a request to another URL.

How do I redirect in Rails?

Rails's redirect_to takes two parameters, option and response_status (optional). It redirects the browser to the target specified in options. This parameter can be: Hash - The URL will be generated by calling url_for with the options.

Does redirect to stop execution?

Please note that you may only call render OR redirect, and at most once per action. Also note that neither redirect nor render terminate execution of the action, so if you want to exit an action after redirecting, you need to do something like "redirect_to(...) and return".


1 Answers

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.

like image 85
Rebitzele Avatar answered Oct 06 '22 03:10

Rebitzele