开发者

Rails: When the "flash" hash becomes empty?

开发者 https://www.devze.com 2023-02-07 09:55 出处:网络
On index page of MyController I set a value to flash[:notice]: class MyController < ApplicationController

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 t开发者_如何学Gohe correct way to empty flash[:notice] ?


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


Most of the time this rule should be correct:

Use flash[:notice] with redirect

Use flash.now[:notice] with render

0

精彩评论

暂无评论...
验证码 换一张
取 消