views:

809

answers:

2

I'm using a flash notice in a Rails application, with the following code:

flash[:notice] = "Sorry, we weren't able to log you in with those details."
render :action => :new

The flash message renders as expected on the 'new' action, but then it also shows on the next page the user visits (whatever that might be). It should only show once, but something's making it stick around.

+2  A: 

Ok, I solved this. The way to get around it is to use:

flash.now[:notice] = "Sorry, we weren't able to log you in with those details."
render :action => :new

The key part being flash.now[:notice] instead of flash[:notice].

Paul Farnell
+12  A: 

There are two way to solve this problem. One is to use

flash.now[:notice]

when your flash must be discarded at the end of the current request and is not intended to be used after a redirect.

The second one is to call

flash.discard(:notice)

at the end of the request. Which one is the best depends on your application behavior.

http://api.rubyonrails.org/classes/ActionController/Flash/FlashHash.html

Simone Carletti