简体   繁体   中英

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, ie 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

您在下面尝试过吗?

redirect_to(root_path, {:flash => {:error => "x"}})

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!

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.

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, ie 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM