Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: When the "flash" hash becomes empty?

On index page of MyController I set a value to flash[:notice]:

class MyController < ApplicationController
  def index
    flash[:notice] = "my message"
    ...
  end
end

I do see "my message" displayed as expected.

However, when I click a link on this page that points to index page of MyOtherController, I still see "my message":

class MyOtherController < ApplicationController
  def index
    puts "----------------------------------------"
    puts flash[:notice]    # => "my message"
    puts "----------------------------------------"
  end
end

I thought that flash[:notice] becomes empty with every request, but here this is not the case. What is the correct way to empty flash[:notice] ?

like image 835
Misha Moroshko Avatar asked Jan 31 '11 07:01

Misha Moroshko


2 Answers

you can use flash.now[:notice] = ... instead. flash.now is useful when you dont want the flash message to persist to the next request. Often a redirect_to follows a flash[:notice] = ... which is why it is persisted for one request

like image 186
joshnuss Avatar answered Nov 16 '22 01:11

joshnuss


Most of the time this rule should be correct:

Use flash[:notice] with redirect

Use flash.now[:notice] with render

like image 3
PeterWong Avatar answered Nov 16 '22 00:11

PeterWong