Rails Flash 的行为与 Rails 指南中所说的不同
Rails Flash doesn't behave as what the Rails Guide says
根据官方flash section on Rails Guide:
The Flash
The flash is a special part of the session which is cleared with each request. This means that values stored there will only be available in the next request, which is useful for passing error messages etc.
...
flash.now
By default, adding values to the flash will make them available to the next request, but sometimes you may want to access those values in the same request.
但是,经过一些测试,我发现 flash
在当前和下一个请求中是可用的。并且 flash.now
在当前请求中 只有 可用。
Rails 指南不正确吗?或者也许我错过了什么?
# Controller action
def test_flash
flash[:notice] = "This is a flash msg."
end
# test_flash.html.erb
<% flash.each do |name, msg| -%>
<%= content_tag :div, msg, class: name %>
<% end -%>
你没看错。 flash.now
仅适用于当前请求。 flash
(没有现在)是唯一可用于下一个请求的。
你上面看到的这句话-其实是在说平淡的flash
(开头),然后才在段末引入flash.now
作为对照。
我不会说文档不正确,但我同意它可以更清楚。
它的措辞应该类似于:
Flash is clear after next request, therefore it's available in the current
request and next request. If you only want it be available in the current
request, not along with the next request, use flash.now
.
另一方面,你不觉得现在没有什么东西吗?
请求,给定当前的 flash API,作为一个类似哈希的对象,这将是相当
诡异的?例如:
flash[:notice] = 'nnf'
puts flash[:notice] # => nil
如果是这样的话,write
和 read
会更有意义。
根据官方flash section on Rails Guide:
The Flash
The flash is a special part of the session which is cleared with each request. This means that values stored there will only be available in the next request, which is useful for passing error messages etc.
...
flash.now
By default, adding values to the flash will make them available to the next request, but sometimes you may want to access those values in the same request.
但是,经过一些测试,我发现 flash
在当前和下一个请求中是可用的。并且 flash.now
在当前请求中 只有 可用。
Rails 指南不正确吗?或者也许我错过了什么?
# Controller action
def test_flash
flash[:notice] = "This is a flash msg."
end
# test_flash.html.erb
<% flash.each do |name, msg| -%>
<%= content_tag :div, msg, class: name %>
<% end -%>
你没看错。 flash.now
仅适用于当前请求。 flash
(没有现在)是唯一可用于下一个请求的。
你上面看到的这句话-其实是在说平淡的flash
(开头),然后才在段末引入flash.now
作为对照。
我不会说文档不正确,但我同意它可以更清楚。 它的措辞应该类似于:
Flash is clear after next request, therefore it's available in the current request and next request. If you only want it be available in the current request, not along with the next request, use
flash.now
.
另一方面,你不觉得现在没有什么东西吗? 请求,给定当前的 flash API,作为一个类似哈希的对象,这将是相当 诡异的?例如:
flash[:notice] = 'nnf'
puts flash[:notice] # => nil
如果是这样的话,write
和 read
会更有意义。