How to prevent browsers from caching a page in Rails

If you have devise configured in your web application.You will find a common problem i.e
when your are on a specific page after login and click browser’s back button which shows last cached page(the after login page).

There is only way to fix it is to remove the caching from the logged in pages for preventing this behavior.

In application_controller.rb



before_filter :clear_browser_cache

def clear_browser_cache
    response.headers["Pragma"] = "no-cache"
    response.headers["Cache-Control"] = "no-cache, no-store, max-age=0, must-revalidate"    
    response.headers["Expires"] = "Fri, 01 Jan 1970 00:00:00 GMT"
end

Hope it will save your time.Thanks.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s