'Rails flash :error, :notice are lost on redirect but alert is not
The docs for redirect_to state clearly that redirect_to
can take :alert => "x"
and :notice=>"x"
options for flash after the path, but anything else needs to go in 'a general purpose flash bucket.'
in my application we often use flash[:error] so I tried to do this
redirect_to root_path, :flash => {:error => "x"}
but the error is not shown on the redirected page.
I have tested without a redirect, i.e. in a normal render, and flash[:error]="x"
results in a successful showing of the message 'x' so it seems clear that this is not a problem in the view but here is the relevant code anyway:
- flash.each do |name, msg|
= content_tag :div, msg, :id => "flash_#{name}"
I have also tried flash.keep between redirects but the flash bucket is always lost.
Also this error has only recently surfaced, so it seems it may be related to an upgrade of some gem - the rails version however has not changed. I am using Rails 4.1.6
Also - I just realised that :notice is being filtered out in a similar way to error, I have had to go through my code and replace notice with :success - very confused, I checked the gem version of actionpack where Flash is coded and it is the same as it used to be when this all was working
bundle exec bundle show actionpack
.....shared/bundle/ruby/2.3.0/gems/actionpack-4.1.6
Solution 1:[1]
Have you tried below?
redirect_to(root_path, {:flash => {:error => "x"}})
Solution 2:[2]
An excerpt from "The Rails 4 Way"
“New to Rails 4, is the ability to register your own flash types by using the new ActionController::Flash.add_flash_types macro style method.”
class ApplicationController
...
add_flash_types :error
end
“When a flash type is registered, a special flash accessor similar to alert and notice, becomes available to be used with redirect_to.”
redirect_to post_url(@post), error: "Something went really wrong!
Solution 3:[3]
I made the following changes:
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload', nonce: true %>
To:
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
I removed the nonce: true in the stylesheet tag and it worked like a charm.
Solution 4:[4]
actually works with
redirect_to(whiteboards_path, flash: {warning: "x"} )
but not with
redirect_to(whiteboards_path, flash: {error: "x"} )
it seems as though with the redirect, something happens to flash to remove the error key and value, and this something has only been introduced recently, either into some gem or into our code.
I also tried
flash[:error] = "x"
redirect_to some_path and return
which does not work but
flash[:error] = "x"
render some_template
works fine, i.e. the error key and value is not removed
so this is a poor answer for my own question but I will accept it if no one comes up with something better:
use alert: "x"
when redirecting instead of flash:{:error=>"x"}
because something removes the error key val in the flash bucket
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | YTorii |
Solution 2 | chaimann |
Solution 3 | Adamu Dankore Muhammad |
Solution 4 | ryan2johnson9 |