繁体   English   中英

如何在Rails中redirect_to一个新呈现的页面(未缓存)?

[英]How do I redirect_to a newly rendered page (not cached) in Rails?

在Rails 3(带有Chrome)中,我设置了Flash,调用redirect_to,因此将浏览器发送回用户之前所在的页面。

但是,Chrome不会呈现此页面。 Chrome会从缓存中获取它。 例如,这意味着未渲染闪光灯。

我可以在URL中添加一个随机参数,以确保不使用缓存,但这很笨拙。

如何确保重新定向到我的页面?

我可以想到几种方法来做到这一点: meta http-equiv标签或http标头。

方法A)meta http-equiv

将以下内容添加到模板的开头部分。

<% if @prevent_caching %>
  <meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate"/>
  <meta http-equiv="Pragma" content="no-cache"/>
  <meta http-equiv="Expires" content="0"/>
<% end %>

然后,在任何控制器操作中,您都可以在呈现页面之前说@prevent_caching = true

这似乎有点笨拙,我相信meta http-equiv可能不可靠。 因此...

方法B)http标头。 这是一种告诉浏览器不要缓存的更加直接和可靠的方法:请参阅如何在所有浏览器中控制网页缓存?

我会将它们放在您的ApplicationController中的一个受保护的方法中,这将使您可以从任何其他控制器调用它(因为它们都继承自此)

#in app/controllers/application.rb
protected
def prevent_browser_caching
  response.headers['Cache-Control'] = 'no-cache, no-store, must-revalidate' # HTTP 1.1.
  response.headers['Pragma'] = 'no-cache' # HTTP 1.0.
  response.headers['Expires'] = '0' # Proxies.
end

然后,就像之前一样,只不过您调用方法而不是设置变量。

#in the controller where you don't want the response to be cached
prevent_browser_caching

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM