How to make the schema of the URL generated by Rails URL helper https

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

How to make the schema of the URL generated by Rails URL helper https
What to do if you don't like the code generated by swagger-codegen-cli
How to make the schema of the URL generated by Rails URL helper https
[Rails] How to display the list of posts by category
[Rails] How to get the URL of the transition source and redirect
[Rails] How to decide the destination by "rails routes"
[Rails] How to convert the URI of the image sent by http to https when using Twitter API
How to decorate the radio button of rails6 form_with (helper) with CSS
[Ruby on Rails] How to make the link destination part of the specified id
[Rails6] How to connect the posting function generated by Scaffold with the user function generated by devise
How to make a unique combination of data in the rails intermediate table
[Java] How to get the URL of the transition source
How to remove the underline displayed by Rails link_to
[Rails] How to change the column name of the table
[Rails] How to get the contents of strong parameters
[Rails] How to make seed
Let's summarize how to extend the expiration date of Rails
[Rails / Heroku / MySQL] How to reset the DB of Rails application on Heroku
[Rails] How to omit the display of the character string of the link_to method
[Rails] How to use the map method
How to determine the number of parallels
How to sort the List of SelectItem
The process of introducing Vuetify to Rails
[Rails] How to use helper method, confimartion
How to solve the local environment construction of Ruby on Rails (MAC)!
[Ruby On Rails] How to search the contents of params using include?
[Rails] How to solve the time lag of created_at after save method
How to find the cause of the Ruby error
How to embed and display youtube videos in Rails (You can also get the URL you entered by editing)
[Rails] Button to return to the top of the page
[Rails] Introduction of pry-rails ~ How to debug binding.pry
Customize how to divide the contents of Recyclerview
Make a margin to the left of the TextField
How to display the amount of disk used by Docker container for each container
How to output CSV created by Rails to S3
Add arbitrary parameters to the URL output by <h: outputScript> <h: outputStylesheet> of JSF
[swift5] How to transition from the app to an external site by specifying the URL
[Ruby on Rails] Rails tutorial Chapter 14 Summary of how to implement the status feed
How to prevent direct URL typing in Rails
How to get today's day of the week
Output of how to use the slice method
How to separate .scss by controller in Rails
How to use JQuery in js.erb of Rails6
[Rails] How to operate the helper method used in the main application with Administrate
How to display the result of form input
[Java] How to get the redirected final URL
[Ruby on Rails] How to Japaneseize the error message of Form object (ActiveModel)
How to make a follow function in Rails
[Java] How to get the authority of the folder
How to check Rails commands in the terminal
How to install Docker in the local environment of an existing Rails application [Rails 6 / MySQL 8]
How to request by passing an array to the query with HTTP Client of Ruby
[ruby] How to assign a value to a hash by referring to the value and key of another hash
[Rough explanation] How to separate the operation of the production environment and the development environment with Rails
[Java] How to display the day of the week acquired by LocalDate, DateTimeformatter in Japanese
[Rails] How to display the weather forecast of the registered address in Japanese using OpenWeatherMap
A review of the code used by rails beginners
How to delete / update the list field of OneToMany
How to set the display time to Japan time in Rails
[Rails] How to search by multiple values ​​with LIKE
[Rails] How to create a signed URL for CloudFront
How to return to the previous screen by Swipe operation