我试图防止浏览器缓存在表单页面上,并阻止用户在提交后返回。
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
another_controller.rb
before_filter :set_no_cache, only: [:new, :edit]
例如,当在“ 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"}
但这对我不起作用,我尝试在适用于Chrome,Opera和Safari浏览器的Ubuntu上进行测试。