Rails: redirect_to with :error, but flash[:error] empty

Daniel picture Daniel · Sep 22, 2011 · Viewed 90.4k times · Source

I'm trying to do a redirect while setting the flash[:error] value. (Rails 3.0.10)

In my view I have

<p id="error"><%= flash[:error] %></p>
<p id="notice"><%= flash[:notice] %></p>

If I do a redirect_to show_path, :notice => "ok" it works fine, but if I do redirect_to show_path, :error => "error" it doesn't show up.

what could I be missing?

Answer

Tarnschaf picture Tarnschaf · Jan 9, 2012

As stated in the Rails API only :notice and :alert are by default applied as a flash hash value. If you need to set the :error value, you can do it like this:

redirect_to show_path, flash: { error: "Insufficient rights!" }