How to prevent browser page caching in Rails How to prevent browser page caching in Rails ruby-on-rails ruby-on-rails

How to prevent browser page caching in Rails


Finally figured this out - http://blog.serendeputy.com/posts/how-to-prevent-browsers-from-caching-a-page-in-rails/ in application_controller.rb

After 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"  endend

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"  endend


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 the rails helper methods like

stylesheet_link_tag

and leave the default settings on your webserver, the assets are typically cached pretty well.