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

RoundOutTooSoon picture RoundOutTooSoon · Dec 20, 2011 · Viewed 17.2k times · Source

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?

Answer

CHsurfer picture CHsurfer · Jan 28, 2012

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!