Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 689
RoundOutTooSoon Avatar asked Dec 20 '11 18:12

RoundOutTooSoon


People also ask

How does flash work in Rails?

Per the Rails Docs, flash is a middleware method that sends forward temporary datatypes which the Rails controller delegates to the request object. Now in plain English: flash is a method through which you can send a temporary string, array, or hash once between your otherwise stateless HTTP requests.


1 Answers

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!

like image 105
CHsurfer Avatar answered Nov 13 '22 11:11

CHsurfer