# Session or flash

**URL:** <https://www.ruby-forum.com/t/session-or-flash/171365>\
**Category:** Rails\
**Created:** [July 1, 2009, 6:22am UTC](https://www.ruby-forum.com/t/session-or-flash/171365 "2009-07-01T06:22:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Xiahong\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/3d9bf3/32.png) [@Xiahong\_G](https://www.ruby-forum.com/u/Xiahong_G)\
**Post date:** [July 1, 2009, 6:22am UTC](https://www.ruby-forum.com/t/session-or-flash/171365/1 "2009-07-01T06:22:18Z")

</div>

Hi everyone,

What’s the difference between Flash and Session? Probably the most  
common use of the flash is to pass error and informational strings  
from one action to the next. But I wonder why I can’t use session for  
this purpose. For example, I found the following piece of code in  
“Agile web development with rails”.

Controller:  
class BlogController  
def display  
@article = Article.find(params[:id])  
end  
def add\_comment  
@article = Article.find(params[:id])  
comment = Comment.new(params[:comment])  
@article.comments \<\< comment  
if @article.save  
flash[:notice] = “Thank you for your valuable comment”  
else  
flash[:notice] = “We threw your worthless comment away”  
end  
redirect\_to :action =\> ‘display’  
end  
end

the following is the layout of the template view:

 My Blog \<%= stylesheet\_link\_tag("blog" ) %\> 
 \<% if flash[:notice] -%\> 
\<%= flash[:notice] %\>
 \<% end -%\> \<%= yield :layout %\> 

My question is why we dont use session here to display error  
information

---

<div class="post-metadata">

**Author:** ![Xiahong\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/3d9bf3/32.png) [@Xiahong\_G](https://www.ruby-forum.com/u/Xiahong_G)\
**Post date:** [July 1, 2009, 6:36am UTC](https://www.ruby-forum.com/t/session-or-flash/171365/2 "2009-07-01T06:36:58Z")

</div>

On Jul 1, 12:21 am, Xiahong G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi everyone,
> 
> What’s the difference between Flash and Session? Probably the most  
> common use of the flash is to pass error and informational strings  
> from one action to the next. But I wonder why I can’t use session for  
> this purpose. For example, I found the following piece of code in  
> “Agile web development with rails”.

The flash is stored in the session. The only thing that is special  
about the flash is that rails takes care of expiring items from it for  
you.

Fred

---

<div class="post-metadata">

**Author:** ![Xiahong\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/3d9bf3/32.png) [@Xiahong\_G](https://www.ruby-forum.com/u/Xiahong_G)\
**Post date:** [July 1, 2009, 6:38am UTC](https://www.ruby-forum.com/t/session-or-flash/171365/3 "2009-07-01T06:38:09Z")

</div>

On Jun 30, 2009, at 9:21 PM, Xiahong G. wrote:

> Hi everyone,
> 
> What’s the difference between Flash and Session? Probably the most  
> common use of the flash is to pass error and informational strings  
> from one action to the next. But I wonder why I can’t use session for  
> this purpose. For example, I found the following piece of code in  
> “Agile web development with rails”.

session isn’t cleared from request to request. flash is. You could  
certainly use session, but you’d need to make sure you did something  
like session[:notice] = nil in a before filter otherwise you’d spit  
out the same message over and over and over.

-philip

---

<div class="post-metadata">

**Author:** ![Xiahong\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/3d9bf3/32.png) [@Xiahong\_G](https://www.ruby-forum.com/u/Xiahong_G)\
**Post date:** [July 1, 2009, 7:24am UTC](https://www.ruby-forum.com/t/session-or-flash/171365/4 "2009-07-01T07:24:56Z")

</div>

thanks all…

2009/6/30 Philip H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
