Change protocol to https in all rails helpers

后端 未结 9 1584
情书的邮戳
情书的邮戳 2020-12-10 12:55

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

相关标签:
9条回答
  • 2020-12-10 13:35

    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
    
    0 讨论(0)
  • 2020-12-10 13:37

    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

    0 讨论(0)
  • 2020-12-10 13:43

    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
    
    0 讨论(0)
提交回复
热议问题