How to prevent browser page caching in Rails
Use:
expires_now()
expires_now()
I have used this line with some success in the controller. It works in Safari and Internet Explorer, but I haven't seen it work with Firefox.
response.headers["Expires"] = "#{1.year.ago}"
For your second point, if you use the Ruby on Rails helper methods like
stylesheet_link_tag
and leave the default settings on your web server, the assets are typically cached pretty well.
I finally figured this out - http://blog.serendeputy.com/posts/how-to-prevent-browsers-from-caching-a-page-in-rails/ in application_controller.rb
.
After Ruby on Rails 5:
class ApplicationController < ActionController::Base
before_action :set_cache_headers
private
def set_cache_headers
response.headers["Cache-Control"] = "no-cache, no-store"
response.headers["Pragma"] = "no-cache"
response.headers["Expires"] = "Mon, 01 Jan 1990 00:00:00 GMT"
end
end
Ruby on Rails 4 and older versions:
class ApplicationController < ActionController::Base
before_filter :set_cache_headers
private
def set_cache_headers
response.headers["Cache-Control"] = "no-cache, no-store"
response.headers["Pragma"] = "no-cache"
response.headers["Expires"] = "Mon, 01 Jan 1990 00:00:00 GMT"
end
end