开发者

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 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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜