10

In one controller I have

flash[:error] = "Message"
redirect_to :root

The :root is handled by another controller, the view has

<% if flash[:error] %>
  <p><%= flash[:error] %></p>
<% end %>

But nothing is being shown. I inserted <%= debug controller.session %>, here's what I got

"flash"=>#<ActionDispatch::Flash::FlashHash:0x2e79208 @used=#<Set: {}>, @closed=false, @flashes={}, @now=nil>}

What did I do wrong?

Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367
kimkunjj
  • 946
  • 1
  • 10
  • 21
  • 1
    What does your routes file and the controller action that root goes to look like? I tried to replicate this, but it worked for me, so maybe those will help to figure it out. – Jeff Smith Apr 01 '12 at 14:06
  • 1
    Jeff, thanks for pointing out the route issue. It turns out that my buddy put in another redirect for the root_handler. After I added flash.keep before the second redirect, the error shows up. – kimkunjj Apr 01 '12 at 18:05
  • just had the same problem in a post action, did you solve it kimkunji? – Moh Mar 07 '15 at 09:06
  • See my comment above. It turns out that my buddy put in another redirect for the root_handler. After I added flash.keep before the second redirect, the error shows up. – kimkunjj Mar 08 '15 at 20:54

3 Answers3

5

I know this is late, but I had the same problem in Rails 4. If you use the _url helper in the redirect_to, the flash message will come through:

def update_post
    respond_to do |format|
        if @post.update(post_params)
            format.html { redirect_to show_post_meta_url, notice: 'Post was successfully updated.' }
        else
            format.html { render action: 'edit_post' }
        end
    end
end

Hope this helps someone.

Astockwell
  • 1,498
  • 13
  • 11
5

Update (2019): This answer might not be up to date according to comments.

Check this question: Rails: redirect_to with :error, but flash[:error] empty .

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!" }
Jonathan Allard
  • 18,429
  • 11
  • 54
  • 75
Justin D.
  • 4,946
  • 5
  • 36
  • 69
-1

This is how I display alerts and notices in my controller

redirect_to user_attachments_path, notice: "The file #{@attachment.name} has been uploaded."

Use flash[:error] or flash[:notice] when you are rendering over the current page and not redirecting like here:

if params[:attachment].nil?
      flash.now[:alert] = "No file found!"
      render "new"
else
Sinstein
  • 887
  • 11
  • 42
  • render 'new' doesn't work because it loses the reference to the record.new and redirect_to some_url(@record), notice: "message" loses the format flash provides – Luis Flores Jan 19 '21 at 20:30
  • @LuisFlores incorrect. The Rails API allows flash messages to be passed directly for notice and alert types. For other types of messages you need to specify the message in the format `redirect_to url flash: {other_type: message}` For all other cases, this solution works. Ref - https://guides.rubyonrails.org/action_controller_overview.html#the-flash – Sinstein Mar 12 '21 at 09:45