简体   繁体   English

Rails flash:错误,:通知在重定向时丢失但警报不是

[英]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.' redirect_to state 的文档清楚地表明redirect_to可以在路径之后为 flash 采用:alert => "x":notice=>"x"选项,但其他任何东西都需要 go 在“通用 flash 存储桶”中。

in my application we often use flash[:error] so I tried to do this在我的应用程序中,我们经常使用 flash[:error] 所以我尝试这样做

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[:error]="x"导致成功显示消息 'x' 所以很明显这在视图中不是问题但这里是无论如何相关的代码:

- 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.我也尝试过 flash.keep 在重定向之间,但 flash 桶总是丢失。

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.此错误也是最近才出现的,因此它似乎可能与某些 gem 的升级有关 - 但是 rails 版本没有改变。 I am using Rails 4.1.6我正在使用 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另外 - 我刚刚意识到:notice 正在以类似于错误的方式被过滤掉,我不得不通过我的代码将 go 替换为:success - 非常困惑,我检查了 actionpack 的 gem 版本,其中 Flash 被编码并且它与这一切正常时的情况相同

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" 摘录自《 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.” “ Rails 4的新功能是可以使用新的ActionController :: Flash.add_flash_types宏样式方法来注册自己的闪存类型。”

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一起使用。”

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.我删除了样式表标签中的nonce: true ,它的效果非常好。

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. 似乎通过重定向,发生了一些事情,以清除错误键和值,并且这些事情直到最近才被引入到gem或我们的代码中。

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 重定向而不是使用flash:{:error=>"x"}时使用alert: "x" ,因为某些操作会删除闪存存储桶中的错误密钥val

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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