Rails: When does a flash hash become empty?

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

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

I see "my message" as expected.

However, when I click on the link on this page that points to the 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 flash[:notice] becomes empty with every request, but this is not the case here. What is the correct way to reset flash[:notice] ?

+4
source share
2 answers

you can use flash.now[:notice] = ... flash.now is useful when you do not want the flash message to persist until the next request. Often a redirect_to follows flash[:notice] = ... , so it is saved for a single request

+7
source

In most cases, this rule should be correct:

Use flash[:notice] with redirect

Use flash.now[:notice] with render

+2
source

Source: https://habr.com/ru/post/1337730/


All Articles