Rails:当“flash”散列变为空时?
问题描述:
在index
页MyController
我的值设置为flash[:notice]
:Rails:当“flash”散列变为空时?
class MyController < ApplicationController
def index
flash[:notice] = "my message"
...
end
end
我确实看到"my message"
按预期显示。
然而,当我点击这个页面指向index
页MyOtherController
上的链接,我仍然看到"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] = ...
这就是为什么它是持续了一个请求
答
大多数时候这个规则应该是正确的:
使用flash[:notice]
与redirect
使用flash.now[:notice]
与render
+0
那么在`redirect`之前`flash [:notice]`和'render`之前是一样的? – 2011-01-31 11:45:36
所以,当我只使用`flash [:notice]`时,它什么时候变空了?有时它变得空虚,有时不变。你能详细解释一下吗? – 2011-02-01 10:18:11