---
title: "9.1 about flash.now[:error] vs flash[:error]"
author: "peryt"
date: "2011-10-12T15:37:58Z"
tags: []
categories: ["编程语言"]
slug: "9-1-about-flash-now-error-vs-flash-error"
draft: false
lastmod: "2011-10-12T15:38:19Z"
id: 1190785
url: "https://peryt.iteye.com/blog/1190785"
---

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](http://ruby.railstutorial.org/chapters/sign-in-sign-out#code:signin_failure)), 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 `render`ing rather than `redirect`ing we use `flash.now`instead 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` with`flash.now`.