Change protocol to https in all rails helpers

99miles picture 99miles · Feb 28, 2012 · Viewed 13.1k times · Source

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?

Answer

Michael Koper picture Michael Koper · Feb 28, 2012

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"