Ultra-small story.
URLs generated by Rails' URL helpers will be http
based.
Rails.application.routes.url_helpers.root_url
=> "http://localhost:3000/"
You must specify the protocol
option to make it https
based.
Rails.application.routes.url_helpers.root_url(protocol: 'https')
=> "https://localhost:3000/"
If you don't want to specify the protocol
option every time, you can change the default by setting default_url_options
.
Rails.application.routes.default_url_options[:protocol] = 'https'
By the way, in the next Rails, if Rails.application.config.force_ssl
is true
, the default will be automatically set to https
.
(PR) https://github.com/rails/rails/pull/37480
It's common sense to use https anymore, so I'm reminded now, but until the next Rails is released, it's a good idea to write it under config / initializers
.
config/initializers/protocol_default_to_https.rb
# force_If ssl is true, set the URL to https.
if Rails.application.config.force_ssl
Rails.application.routes.default_url_options[:protocol] = 'https'
end
Recommended Posts