Uncacheable web pages in Rails
Posted by Peter Donald Thu, 15 Dec 2005 03:36:00 GMT
In an attempt to ensure that a page is always fetched from the server I needed to add the following code to my base controller. Seems to be a bit long-winded but does the job.
class ApplicationController < ActionController::Base
before_filter :force_no_cache
private
def force_no_cache
# set modify date to current timestamp
response.headers["Last-Modified"] = CGI::rfc1123_date(Time.now)
# set expiry to back in the past
# (makes us a bad candidate for caching)
response.headers["Expires"] = 0
# HTTP 1.0 (disable caching)
response.headers["Pragma"] = "no-cache"
# HTTP 1.1 (disable caching of any kind)
# HTTP 1.1 'pre-check=0, post-check=0' (IE specific)
response.headers["Cache-Control"] =
"no-cache, no-store, must-revalidate, pre-check=0, post-check=0"
end