ITeye博客

9.1 about flash.now[:error] vs flash[:error]

peryt · 2011-10-12 15:37 · ruby and rails · 编程语言 · Markdown

There’s a subtle difference between flash and flash.now. The flash variable is designed to be used before a redirect, and it persists on the resulting page for one request—that is, it appears once, and disappears when you click on another link. Unfortunately, this means that if we don’t redirect, and instead simply render a page (as in Listing 9.8), the flash message persists for two requests: it appears on the rendered page but is still waiting for a “redirect” (i.e., a second request), and thus appears again if you click a link.

To avoid this weird behavior, when rendering rather than redirecting we use flash.nowinstead of flash. The flash.now object is specifically designed for displaying flash messages on rendered pages. If you ever find yourself wondering why a flash message is showing up where you don’t expect it, chances are good that you need to replace flash withflash.now.

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。