Search code examples
ruby-on-railscachingpage-caching

Rails page caching and flash messages


I'm pretty sure I can page cache the vast majority of my site but the one thing preventing me from doing so is that my flash messages will not show, or they'll show at the wrong time.

One thing I'm considering is writing the flash message to a cookie, reading it and displaying it via javascript and clearing the cookie once the message has been displayed. Has anyone had any success doing this or are there better methods?

Thanks.


Solution

  • One solution would be to cache the page, but include a javascript snippet that will make another small request just for the section you want to be dynamic. So the user will download the page fully, and then when javascript executes, it will pull down the dynamic page element.

    I wrote a short blog post about this a while back. http://chase.ratchetsoftware.com/2008/12/rails-caching-dynamic-fragments/

    Also, Greg Pollack of RailsEnvy did a screencast where he focuses on having dynamic data in cached pages. http://railslab.newrelic.com/2009/02/05/episode-5-advanced-page-caching

    Hope this helps,

    Chase Gray