每个render / redirect_to调用的基本协议(https://)

时间:2012-05-14 09:20:28

标签: ruby https protocols render redirecttoaction

有没有办法设置基本协议才能使用

render :action => "myaction"
redirect_to :action => "myaction"

而不是打电话

render :action => "myaction", :protocol => "https://"
redirect_to :action => "myaction", :protocol => "https://"

每一次?

1 个答案:

答案 0 :(得分:-1)

只需在您的环境配置中使用config.force_ssl = true。

# config/application.rb
module MyApp
  class Application < Rails::Application
    config.force_ssl = true
  end
end

您还可以根据当前的Rails环境有选择地启用https。例如,您可能希望在开发时关闭HTTPS,并在登台/生产时启用它。

# config/application.rb
module MyApp
  class Application < Rails::Application
    config.force_ssl = false
  end
end

# config/environments/production.rb
MyApp::Application.configure do
  config.force_ssl = true
end

在幕后,Rails将令人敬畏的Rack :: SSL Rack中间件添加到您的应用程序中间件堆栈中。 Rack :: SSL会自动过滤请求,将非HTTPS请求重定向到相应的HTTPS路径,并应用一些其他改进以确保您的HTTPS请求是安全的。