Rails:当“flash”散列变为空时?

问题描述:

indexMyController我的值设置为flash[:notice]Rails:当“flash”散列变为空时?

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

我确实看到"my message"按预期显示。

然而,当我点击这个页面指向indexMyOtherController上的链接,我仍然看到"my message"

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

我认为flash[:notice]变空每个请求,但在这里,这不是案件。什么是清空flash[:notice]的正确方法?

您可以改用flash.now[:notice] = ...flash.now在您不希望Flash消息持续到下一个请求时非常有用。一个redirect_to通常遵循flash[:notice] = ...这就是为什么它是持续了一个请求

+0

所以,当我只使用`flash [:notice]`时,它什么时候变空了?有时它变得空虚,有时不变。你能详细解释一下吗? – 2011-02-01 10:18:11

大多数时候这个规则应该是正确的:

使用flash[:notice]redirect

使用flash.now[:notice]render

+0

那么在`redirect`之前`flash [:notice]`和'render`之前是一样的? – 2011-01-31 11:45:36