rornoob.com >

Rails flash[] messages not clearing


16-Oct-2009

I'm sure you're aware of flash[:notice] - that friendly way to pass messages on to your views, even when an HTTP redirect is involved.

I was happily using flash[:notice] to display a Changes Saved message to users when they successfully updated their profile but after making some additional changes I began encountering a perplexing problem - sometimes the flash messages would "stick around" for a while - i.e. they weren't always clearing themselves out automatically.

After some digging around it seems the answer is a simple one. In fact you probably already know it. (Well maybe not if you've read this far...)

The answer comes from the ruby on rails help pages themselves:

"When you need to pass an object to the next action, you use the standard flash assign ([]=). When you need to pass an object to the current action, you use now, and your object will vanish when the current action is done."

Ok, that explains it. My old code was using flash[...] in combination with redirect_to but my new code was using it with render. i.e. the messages weren't being cleared out properly because rails didn't consider my renders to be new pages views.

So to summarise, use:

  • flash[:notice] with redirects (e.g. redirect_to :action => 'something')
  • flash.now[:notice] with renders (i.e. render :action => 'something')

Result: No more flash clearing issues.

|

del.icio.usDiggblogmarksNewsvineStumbleUponTechnoratiFurlReddit

Comments

RoR N00b said on 21-Mar-2011, "Thanks":
Thanks. Looks like you have a pretty new blog, but I found this entry useful, and wanted to tell you to keep it going!
Add your comment
Name
Website
Heading
Comment