我想在生产中将所有站点流量从所有 Controller 重定向到 https。可能使用如下内容:
before_filter :redirect_to_https
在 Controller 中使用以下内容:
def redirect_to_https
redirect_to :protocol => "https://" unless (request.ssl? || local_request? || Rails.env.development? || Rails.env.staging? ) # probably don't need redundant development with `local_request` present
end
与必须在每个 Controller 中重复此操作相比,将此应用到所有 Controller 的最有效方法是什么?
最佳答案
在您的 environments/production.rb 配置文件中使用 config.force_ssl = true
。
如果你想要一个 before_filter 应用到所有的 Controller ,它们都继承自 ApplicationController 所以把它放在那里。
关于ruby-on-rails - 当需要应用于整个应用程序时,如何在每个 Controller 中不重复 `before_filter` 代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10058444/