5

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?

2 Answers 2

8

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!

4

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)

3
  • thanks for your reply. But I also tried this and it doesn't work for :warning and :error but only :notice. Dec 20, 2011 at 19:25
  • Then you need to show more of what you're doing: what is setting the flash (and where), where is it being displayed etc. Dec 20, 2011 at 19:28
  • Hi Frederick, thanks for getting back to me. I'm using flash.now so that I can display flash messages without redirecting. So I set the message to the flash on the same view like I showed in my pasted code. And it's on the top of the form, it's visible to viewer. Dec 20, 2011 at 22:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.