Sohan's Blog

Things I'm Learning

flash.now - Did You Know?

Well, I didn’t know this until late. If you are like me, you have often wondered when you saw those unwanted flash messages appearing after a request, sometimes after ajax requests when you do a redirect, all on a sudden that flash shows up!
Well, as you have thought, the Ruby on Rails people have solved this problem way before now. The solution is to wisely use flash.now[] and flash[]. Here are a few tips:

Use flash.now[] when you are flashing:

  • on ajax requests
  • before a call to render :action => :my_action, as often found in failure cases of create and update actions
Use flash[] when you are flashing:

  • before a redirect_to, often found in the success cases of create and update actions
In general, use flash.now[] when you don’t want to carry over the flash to the next http request and flash[] otherwise. The FlashHash class has more detail on this at the ruby on rails API page. Hope this helps you to get rid of those annoying and often embarrassing out-of-context flash messages!

Comments

Sohan
Thanks Severin for your comment and pointing the typo.
Severin
Helpful explanation, thanks!

But there's a little typo: "Use flash.NEW[]…" should be "Use flash.NOW[]…"
Sohan
Hi Arif, you are welcome. Happy to help :-)
Arif
Nice tips. i think we can utilize it in our project to get rid of some mysterious flash messages :) Thank you.
Sohan
You are welcome Ashif.
Ashif Manjur
Thanks for this nice little useful post…