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?
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.
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.
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".
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With