Base protocol (https://) for every render/redirect_to call

329 views Asked by At

is there a way i can set up a base protocol to use

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

instead of calling

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

every time?

1

There are 1 answers

1
Iuri G. On

Simply use config.force_ssl = true in your environment configuration.

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

You can also selectively enable https depending on the current Rails environment. For example, you might want to keep HTTPS turned off on development, and enable it on staging/production.

# 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

Behind the scenes, Rails adds the awesome Rack::SSL Rack middleware to your application middleware stack. Rack::SSL automatically filters the request, redirects not-HTTPS requests to the corresponding HTTPS path and applies some additional improvements to make sure your HTTPS request is secure.