How to display a Rails flash notice upon redirect?
M

7

74

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?

Milstone answered 20/3, 2013 at 20:33 Comment(0)
C
132

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.

Cohabit answered 20/3, 2013 at 20:39 Comment(1)
Perfect! thanks. It's making me wait 6 more minutes to accept the answer for some reason...Milstone
S
45
redirect_to new_user_session_path, alert: "Invalid email or password"

in place of :alert you can use :notice

to display

Snaky answered 21/3, 2013 at 10:52 Comment(0)
K
23

Or you can do it in one line.

redirect_to check_in_path, flash: {notice: "Successfully checked in"}
Karyokinesis answered 12/12, 2014 at 16:10 Comment(4)
@JayEl-Kaake what ruby version are you using?Karyokinesis
It was a while ago, so I'm not sure... I'll redact my comment since it looks like that should work.Cupule
path, flash: {notice: ... seems like Rails 3, while path, alert: ... is Rails 4+Revocable
Nice solution, as it also allows different types of messages, e.g. flash: {success: "text"}Epa
L
13

This will work too

redirect_to check_in_path, notice: 'Successfully checked in'

Local answered 25/5, 2016 at 13:49 Comment(0)
W
11

If you are using Bootstrap, this will display a nicely-formatted flash message on the page that's the target of your redirect.

In your controller:

if my_success_condition
  flash[:success] = 'It worked!'
else
  flash[:warning] = 'Something went wrong.'
end
redirect_to myroute_path

In your view:

<% flash.each do |key, value| %>
  <div class="alert alert-<%= key %>"><%= value %></div>
<% end %>

This will produce HTML like:

<div class="alert alert-success">It worked!</div>

For available Bootstrap alert styles, see: http://getbootstrap.com/docs/4.0/components/alerts/

Reference: https://agilewarrior.wordpress.com/2014/04/26/how-to-add-a-flash-message-to-your-rails-page/

Willumsen answered 5/10, 2017 at 20:41 Comment(0)
S
3

I had the same problem, and your question solved mine, because I had forgotten to include in the /check_in view:

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

In the controller, just a single line:

redirect_to check_in_path, :notice => "Successfully checked in"             
Slothful answered 8/2, 2015 at 18:44 Comment(1)
yours is a different problem. I had the <%= notice %> tag, just nothing was coming upMilstone
M
0

If you redirect using action, action, and flash message must be passed as separate arguments.

    redirect_to({ action: 'show'}, notice: 'Successfully checked in')

Reference: https://api.rubyonrails.org/classes/ActionController/Redirecting.html#method-i-redirect_to

Maramarabel answered 18/6 at 6:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.