How to set config.action_controller.default_url_options = {:host = '#''} on per environment basis

前端 未结 7 851
再見小時候
再見小時候 2020-12-12 22:20

Right now I\'m using this which works for the development host, but I have to manually change the {:host => \"\"} code when I move to production.

post.rb



        
相关标签:
7条回答
  • 2020-12-12 23:00

    config/environments/development.rb (any other environment, same)

    add this row with host that you want

    routes.default_url_options[:host] = 'localhost:3000'
    
    0 讨论(0)
  • 2020-12-12 23:07

    Inherit your Application's default_url_options from ActionMailer.

    You want to keep things as DRY as possible so, ideally, you don't want to hard code your host and port in multiple places for the same environment, unless your ActionMailer actually uses a different host and port than the rest of your Application.

    To set the default_url_options for your entire Application, simply add the following line to your config/environment.rb file (changing MyApp to your app's name):

    # Set the default host and port to be the same as Action Mailer.
    MyApp::Application.default_url_options = MyApp::Application.config.action_mailer.default_url_options
    

    This will fix your problem and automatically set your Application's default_url_options to the same as your config.action_mailer.default_url_options:

    $ MyApp::Application.config.action_mailer.default_url_options
    #=> {:host=>"lvh.me", :port=>"3000"}
    
    $ MyApp::Application.default_url_options
    #=> {:host=>"lvh.me", :port=>"3000"}
    
    0 讨论(0)
  • 2020-12-12 23:19

    config.action_mailer.default_url_options = { :host => "your host" }

    for instance your host localhost:3000

    you can put this in test.rb, development.rb, production.rb files host could be different from environment to environment

    0 讨论(0)
  • 2020-12-12 23:20

    You have to restart your server before the changes to this file takes effect.

    0 讨论(0)
  • 2020-12-12 23:20

    I know this is an old thread, but I ran into this with Ruby 2.6.3 and Rails 5.2.3. The behavior I was seeing was basically that every path I added would fail with Error during failsafe response: undefined method 'empty?' for nil:NilClass. In production it worked fine, but in my development environment, I would get the error mentioned above.


    The fix for me was add this to controllers/application_controller.rb:

    def default_url_options
      if Rails.env.production?
        Rails.application.routes.default_url_options = { host: "www.production-domain.com", protocol: 'https' }
      elsif Rails.env.development?
        Rails.application.routes.default_url_options = { host: 'localhost:3000', protocol: 'http' }
      end
    end
    

    I was then able to run my development environment on my local.

    0 讨论(0)
  • 2020-12-12 23:22

    Okay I figured it out the correct way to write it is

    Rails.application.routes.default_url_options[:host] = 'localhost:3000'
    

    :)

    0 讨论(0)
提交回复
热议问题