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

老子叫甜甜 提交于 2019-12-23 10:17:09

问题


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. 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?


回答1:


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. It seems that you can set :notice and :alert directly in the redirect method, but not :error and :warning.

Testing for flash[:warning].nil? in the next action gives true, but 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:

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

It's not as elegant, but it works!




回答2:


Rails does nothing magic with the contents of the flash, other than empty it when it is supposed to.

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)



来源:https://stackoverflow.com/questions/8580524/rails-flash-with-warning-alert-and-error-not-shown-only-notice-shown

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!