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
On Jul 1, 12:21 am, Xiahong Gao <gaox...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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> 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
On Jun 30, 2009, at 9:21 PM, Xiahong Gao 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
thanks all.. 2009/6/30 Philip Hallstrom <philip-LSG90OXdqQE@public.gmane.org>> > > > On Jun 30, 2009, at 9:21 PM, Xiahong Gao 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 > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---