# Flash\[:notice\]

**URL:** <https://www.ruby-forum.com/t/flash-notice/82794>\
**Category:** Rails\
**Created:** [January 21, 2007, 2:24am UTC](https://www.ruby-forum.com/t/flash-notice/82794 "2007-01-21T02:24:50Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dxk3355](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d6d6ee/32.png) [@dxk3355](https://www.ruby-forum.com/u/dxk3355)\
**Post date:** [January 21, 2007, 2:24am UTC](https://www.ruby-forum.com/t/flash-notice/82794/1 "2007-01-21T02:24:50Z")

</div>

Hello!

I want to understand more about how flash[:notice] works, because in one  
case it stays on longer than I would want or expect…

I set it in on of the actions, and in the view for that action there is  
a link to somewhere else… When the user clicks on that link, the flash  
message is displayed in the new action/view… how come? If I do  
flash.now[:notice] = ‘’… then it works as expected… my questions: is  
it normal that in first case the message stays… and why? what is the  
exact difference between flash and flash.now… is this subject easy to  
understand and consistent or is it a little bit “hackish” and blurry?

The details:

def forgotten\_password  
# if post, generate password reset link and store it in the user  
table, otherwise just show the form  
if request.post?  
user = User.find\_by\_email(params[:email])  
if !user  
flash[:notice] = “There is no user with such email”  
else  
Notifier::deliver\_password\_reset\_key(user)  
# show the message about the sent email  
render :action =\> ‘password\_reset\_sent’  
end  
end  
end

This is the view for this action:

\<%= start\_form\_tag :action =\> ‘forgotten\_password’ %\>  
Plase enter your email:

\<%= text\_field\_tag(:email, ‘’ ) %\>

\<%= submit\_tag ‘OK’ %\>  
\<%= end\_form\_tag %\>

  

\<%= link\_to “Home”, :action =\> ‘login’ %\>

So when I enter the non-existing email, the above flash message is set  
and this view(form) displayed again… BUT when I click on HOME (link on  
the bottom), the message is still there in the ‘login’ action…

as I said - the message goes away if I use flash.now() … but I would  
really like to understand the philosophy of this… I don’t want to go  
testing the code for that kind of stuff and not learning it the right  
way…

THANK YOU A LOT for the clarification!

David

---

<div class="post-metadata">

**Author:** ![dxk3355](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d6d6ee/32.png) [@dxk3355](https://www.ruby-forum.com/u/dxk3355)\
**Post date:** [January 21, 2007, 4:58am UTC](https://www.ruby-forum.com/t/flash-notice/82794/2 "2007-01-21T04:58:08Z")

</div>

A flash is essensial a session variable that is around in the request  
after it was set. On the request after that again, it’s gone. Not sure  
about your problem, though.

On Jan 21, 2:24 am, David K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![dxk3355](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d6d6ee/32.png) [@dxk3355](https://www.ruby-forum.com/u/dxk3355)\
**Post date:** [January 21, 2007, 9:51am UTC](https://www.ruby-forum.com/t/flash-notice/82794/3 "2007-01-21T09:51:33Z")

</div>

You could use flash.now[:notice], that way it will only show once.

Kind regards,

## Nick

> **[Ruby on Rails hosting companies - Compare, review and find your next ROR host](https://www.railshostinginfo.com/)**
>
> Are you looking for a good and reliable Ruby on Rails hosting company? We have lined them up so you can easily compare and review them.

Compare Rails hosting companies

---

<div class="post-metadata">

**Author:** ![dxk3355](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d6d6ee/32.png) [@dxk3355](https://www.ruby-forum.com/u/dxk3355)\
**Post date:** [January 21, 2007, 8:57pm UTC](https://www.ruby-forum.com/t/flash-notice/82794/4 "2007-01-21T20:57:02Z")

</div>

thank you, but this doesn’t tell me anything 😉

I know that… and it does work… but I wanted to know why the normal  
flash doesn’t… I also wanted to know the difference between the two  
(how it works)…

thank you anyway … but can anyone provide the _real_ explanation?  
Thank you!

enjoy!

david

Nick S. wrote:

> You could use flash.now[:notice], that way it will only show once.
> 
> Kind regards,
> 
> ## Nick
> 
> [http://www.railshostinginfo.com](http://www.railshostinginfo.com)  
> Compare Rails hosting companies

---

<div class="post-metadata">

**Author:** ![dxk3355](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d6d6ee/32.png) [@dxk3355](https://www.ruby-forum.com/u/dxk3355)\
**Post date:** [January 22, 2007, 1:47am UTC](https://www.ruby-forum.com/t/flash-notice/82794/5 "2007-01-22T01:47:50Z")

</div>

OK, I got it now…  
The thing that was confusing me was that somehow I thought that regular  
flash is cleared after rendering a view (of course it’s not), when in  
fact it’s the next action that is the final stop…

great! Thank you all!

David

Rob S. wrote:

> You have pretty much answered it yourself.  
> From  
> [Ruby on Rails — A web-app framework that includes everything needed to create database-backed web applications according to the Model-View-Controller (MVC) pattern.](http://www.rubyonrails.org/api/classes/ActionController/Flash.html)
> 
> The flash provides a way to pass temporary objects between actions.  
> Anything you place in the flash will be exposed to the very next action  
> and then cleared out. This is a great way of doing notices and alerts,  
> such as a create action that sets flash[:notice] = “Successfully  
> created” before redirecting to a display action that can then expose the  
> flash to its template. Actually, that exposure is automatically done.
> 
> and for Flash.now  
> [Ruby on Rails — A web-app framework that includes everything needed to create database-backed web applications according to the Model-View-Controller (MVC) pattern.](http://www.rubyonrails.org/api/classes/ActionController/Flash/FlashHash.html)
> 
> now()  
> Sets a flash that will not be available to the next action, only to the  
> current.

---

<div class="post-metadata">

**Author:** ![dxk3355](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d6d6ee/32.png) [@dxk3355](https://www.ruby-forum.com/u/dxk3355)\
**Post date:** [January 21, 2007, 9:06pm UTC](https://www.ruby-forum.com/t/flash-notice/82794/6 "2007-01-21T21:06:24Z")

</div>

You have pretty much answered it yourself.  
From

> **[Ruby on Rails](https://rubyonrails.org)**
>
> A web-app framework that includes everything needed to create database-backed web applications according to the Model-View-Controller (MVC) pattern.

The flash provides a way to pass temporary objects between actions.  
Anything you place in the flash will be exposed to the very next action  
and then cleared out. This is a great way of doing notices and alerts,  
such as a create action that sets flash[:notice] = “Successfully  
created” before redirecting to a display action that can then expose the  
flash to its template. Actually, that exposure is automatically done.

and for Flash.now

> **[Ruby on Rails](https://rubyonrails.org)**
>
> A web-app framework that includes everything needed to create database-backed web applications according to the Model-View-Controller (MVC) pattern.

now()  
Sets a flash that will not be available to the next action, only to the  
current.
