简体   繁体   English

Rails闪烁,警告,警告和错误未显示; 只有通知显示

[英]Rails flash with warning, alert and error not shown; only notice shown

In my view, I have: 在我看来,我有:

<% flash.now[:error] = "ERROR FLASH" %>
<% flash.now[:notice] = "NOTICE FLASH" %>
<% flash.now[:warning] = "WARNING FLASH" %>

When the page gets render, only the blue info box with NOTICE FLASH appears. 页面渲染时,只显示带有NOTICE FLASH的蓝色信息框。 The other two will not be shown. 其他两个将不会显示。 The same thing happens with the equal signs: 同样的事情发生在等号上:

<%= flash.now[:error] = "ERROR FLASH" %>
<%= flash.now[:notice] = "NOTICE FLASH" %>
<%= flash.now[:warning] = "WARNING FLASH" %>

Is there a setting in my rails app that sets warning or error flashes to not appear? 我的rails应用程序中是否有设置警告或错误闪烁未出现?

I was having the same problem with the following code: 我遇到了以下代码的相同问题:

redirect_to(docs_path, :warning => "I am here!!!") and return if @doc.nil?

using ':notice' and ':alert' instead of ':warning' works as expected. 使用':notice'和':alert'而不是':warning'按预期工作。 It seems that you can set :notice and :alert directly in the redirect method, but not :error and :warning. 您似乎可以在重定向方法中直接设置:notice和:alert,但不能:error和:warning。

Testing for flash[:warning].nil? 测试闪光[:警告] .nil? in the next action gives true, but flash[:notice].nil? 在下一个动作中给出了真实,但是flash [:notice] .nil? is false (ie. the :warning flash is not set, but the :notice is set). 是假的(即:未设置警告闪烁,但设置了:通知)。

To get around this I set the flash[:warning] value before the redirect like so: 为了解决这个问题,我在重定向之前设置了flash [:warning]值,如下所示:

if @doc.nil?
  flash[:warning] =  "I am here!!!"
  redirect_to(docs_path) and return 
end

It's not as elegant, but it works! 它不是那么优雅,但它有效!

Rails does nothing magic with the contents of the flash, other than empty it when it is supposed to. Rails对闪存的内容没有任何魔力,除了它应该被清空时。

It's entirely up to you to take appropriate action on the contents of the flash, ie if you want to display error, notice and warning then you have to put 完全取决于你对闪存的内容采取适当的措施,即如果你想显示错误,通知和警告,那么你必须把

<%= flash[:error] %>

Somewhere in your view templates or layouts where the user will be able to see it (and repeat for :warning, :notice and any other flash key that you want displayed in this way) 在您的视图模板或布局中的某个位置,用户将能够看到它(并重复:警告,:通知以及您希望以这种方式显示的任何其他闪存键)

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

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