session or flash

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:   <head>     <title>My Blog</title>     <%= stylesheet_link_tag("blog" ) %>   </head>   <body>     <div id="main" >       <% if flash[:notice] -%>       <div id="notice" ><%= flash[:notice] %></div>       <% end -%>       <%= yield :layout %>     </div>   </body> </html>

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

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

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

thanks all…