简体   繁体   English

Rails Flash的行为与Rails Guide所说的不一样

[英]Rails Flash doesn't behave as what the Rails Guide says

According to the official flash section on Rails Guide : 根据Rails Guide的官方Flash部分

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 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. 默认情况下,向闪存添加值将使它们可用于下一个请求,但是有时您可能希望在同一请求中访问这些值。

However, after doing some test, I found that flash is available in the current & next request. 但是,经过一些测试,我发现当前和下一个请求中都提供了flash And flash.now is only available in the current request. 并且flash.now 在当前请求中可用。

Is the Rails Guide incorrect? Rails指南不正确吗? Or maybe I miss something? 也许我想念什么?

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

I won't say the document is incorrect, but I agree it could be more clear. 我不会说文件不正确,但我同意它可能会更清楚。 It should be phrased something like: 应该这样写:

Flash is clear after next request, therefore it's available in the current request and next request. 下一个请求后Flash会清除,因此在当前请求和下一个请求中都可用。 If you only want it be available in the current request, not along with the next request, use flash.now . 如果只希望它在当前请求中可用,而不在下一个请求中可用,请使用flash.now

On the other hand, don't you think something is not available in the current request, given current flash API, as a hash like object, it would be quite weird? 另一方面,鉴于当前的Flash API,像哈希这样的对象,您是否认为当前请求中不存在某些内容,这会很奇怪吗? For example: 例如:

flash[:notice] = 'nnf'
puts flash[:notice] # => nil

It would make more sense with write and read if that's the case. 它会更有意义与writeread ,如果是这样的话。

You have read it incorrectly. 您没有正确阅读。 flash.now is only available to the current request. flash.now仅适用于当前请求。 flash (without now) is the only one that will be available to the next request. flash (现在没有)是唯一可用于下一个请求的flash

The sentence you read above - is actually talking about plain flash (to begin with), and only then introduces flash.now as a contrast at the end of the paragraph. 您在上面阅读的这句话实际上是在谈论普通flash (首先),然后才在本段末尾引入flash.now作为对比。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM