2011-01-31 51 views
4

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]的正確方法?

回答

7

您可以改用flash.now[:notice] = ...flash.now在您不希望Flash消息持續到下一個請求時非常有用。一個redirect_to通常遵循flash[:notice] = ...這就是爲什麼它是持續了一個請求

+0

所以,當我只使用`flash [:notice]`時,它什麼時候變空了?有時它變得空虛,有時不變。你能詳細解釋一下嗎? – 2011-02-01 10:18:11

2

大多數時候這個規則應該是正確的:

使用flash[:notice]redirect

使用flash.now[:notice]render

+0

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