17

Rails 3.1+ I want my url helpers to use the https protocol without having to specify it in every helper I call. After searching around I've found various ways but none work, for example:

 ROUTES_PROTOCOL = (ENV["RAILS_ENV"] =~ /development/ ? 'http://' : 'https://')

scope :protocol => ROUTES_PROTOCOL, :path => "/app" do

How can this be done?

99miles
  • 10,942
  • 18
  • 78
  • 123

9 Answers9

17

If you are using Rails 4, defining ApplicationController#default_url_options doesn't work. URL options are now defined in the application's routes config:

Rails.application.routes.draw do
  default_url_options protocol: :https
end
iloveitaly
  • 2,053
  • 22
  • 21
14

So you want it mainly for links in emails?

I think this will work in your production.rb, development.rb or any other environment.

config.action_mailer.default_url_options = {
  :host => 'yourwebsite.com',
  :protocol => 'https'
}

# Makes it possible to use image_tag in mails
config.action_mailer.asset_host = "https://yourwebsite.com"
Michael Koper
  • 9,586
  • 7
  • 45
  • 59
13

In Rails 5.1.4, I have tested the following scenarios:

# in development.rb
config.action_controller.default_url_options({:protocol => 'https'})
config.action_controller.default_url_options(:protocol => 'https')
# Does not work

# in development.rb, outside config block
Rails.application.routes.default_url_options[:protocol] = 'https'
# Does not work, but works under console

# in routes.rb
Rails.application.routes.draw do
  default_url_options protocol: :https
# Does not work, but works under console

# in ApplicationController
def default_url_options(options={})
  { secure: true }
end
# Does not work

# in ApplicationController
def default_url_options
  { protocol: :https }
end
# Works in browser, but does not work under console

# in development.rb
config.action_controller.default_url_options= {:protocol => 'https'}
# Works in browser, but does not work under console
lulalala
  • 17,572
  • 15
  • 110
  • 169
4

For a rails 5.2.0 RESTAPI App the following worked:

In each environment file as necessary ie config/environments/test.rb

  Rails.application.routes.default_url_options[:protocol] = 'https'

Controller code:

 Rails.application.routes.url_helpers.url_for(uploaded_file)
FlimFlam Vir
  • 1,080
  • 9
  • 15
2

You can add this code to ApplicationController

  def default_url_options(options={})
    options.merge(protocol: :https)
  end

You can also check out to Getting Rails URL helpers to automatically output https urls

Piotr Galas
  • 4,518
  • 2
  • 21
  • 30
2

I tried all answers above, only this works for me:

config/environments/production.rb

Rails.application.routes.default_url_options[:protocol] = 'https'

ruby 2.1.4p265 (2014-10-27 revision 48166) [x86_64-linux] Rails 3.2.22.5

karl li
  • 1,316
  • 15
  • 19
2

If you want to force SSL on your application, this can be done by setting config.force_ssl to true in your application.rb (or your environment specific file). More on the subject here

EDIT Ok so I don't find enough evidence for this, but I think you can override default_url_options=(options={}) in application controller, and set :protocol => :https in the function body. If this does not the trick for your emails, you'll have to repeat the procedure in your environment config by adding config.action_mailer.default_url_options. Hope this does it!

ksol
  • 11,835
  • 5
  • 37
  • 64
  • 4
    That will cause a redirect when the request protocol is http, but I would like https in url helpers, mainly for links in emails. – 99miles Feb 28 '12 at 21:20
0

Whichever environment you want to use ssl (https://), just add this config lines to its configuration file in config/environments:

YOURAPPNAME::Application.configure do

  # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
  config.force_ssl = true
end
TheDelChop
  • 7,938
  • 4
  • 48
  • 70
  • 5
    That will cause a redirect when the request protocol is http, but I would like https in url helpers, mainly for links in emails. – 99miles Feb 28 '12 at 21:20
0

In Rails 3.2.1, by default force_ssl is true, let's check

  1. Open config/environments/production.rb and search "config.force_ssl"

config.force_ssl = true - no need to change

now in config/environments/development.rb - no need to place config.force_ssl, it should work, because your server running locally.

Ok, here is the other view

if !request.ssl?
  "https://" + request.host + request.request_uri
elsif request.ssl?
  "http://" + request.host + request.request_uri
end

Add def in helper base on above if else and in the ActionView::Helpers, there is a url_for method that might get you what you want if you start using that.

AMIC MING
  • 6,306
  • 6
  • 46
  • 62
  • 1
    That will cause a redirect when the request protocol is http, but I would like https in url helpers, mainly for links in emails. – 99miles Feb 28 '12 at 21:20
  • @99miles - I altered the answer, Please check, let me know if this works for you. – AMIC MING Feb 28 '12 at 22:13