首页 > 解决方案 > 无法阻止浏览器缓存页面

问题描述

我试图阻止浏览器在表单页面上缓存并阻止用户在提交后返回。

application_controller.rb

protected

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

另一个控制器.rb

before_filter :set_no_cache, only: [:new, :edit]

例如,在“编辑”操作上添加一个时byebug,我看到标题正确更改:

(byebug) headers
{"X-Frame-Options"=>"SAMEORIGIN", "X-XSS-Protection"=>"1; mode=block", "X-Content-Type-Options"=>"nosniff", "Cache-Control"=>"no-cache, no-store, max-age=0, must-revalidate", "Pragma"=>"no-cache", "Expires"=>"Fri, 01 Jan 1990 00:00:00 GMT"}

但这对我不起作用,我在 Ubuntu 上尝试了 Chrome、Opera 和 Safari 浏览器。

标签: ruby-on-rails

解决方案


推荐阅读