Rails: Missing host to link to! Please provide :host parameter or set default_url_options[:host]

前端 未结 15 2577
深忆病人
深忆病人 2020-11-27 10:34

I have been googling for about 90 minutes now and still don\'t have an answer to this. Where do I set default_url_options? I\'ve already set it for conf

相关标签:
15条回答
  • 2020-11-27 11:24

    I had this same error. I had everything written in correctly, including the Listing 10.13 from the tutorial.

    Rails.application.configure do
    .
    .
    .
    config.action_mailer.raise_delivery_errors = true
    config.action_mailer.delevery_method :test
    host = 'example.com'
    config.action_mailer.default_url_options = { host: host }
    .
    .
    .
    end
    

    obviously with "example.com" replaced with my server url.

    What I had glossed over in the tutorial was this line:

    After restarting the development server to activate the configuration...

    So the answer for me was to turn the server off and back on again.

    0 讨论(0)
  • 2020-11-27 11:26

    When you use any listing_url method the full URL will be returned(not a relative one as normal). That's why rails is asking you for the host, to compute the whole URL.

    How you can tell rails the host? You can do it in several ways:

    1.Adding this option to each environment:

    [/config/development.rb]
    config.action_mailer.default_url_options = { host: "localhost:3000" }
    [/config/test.rb]
    config.action_mailer.default_url_options = { host: "localhost:3000" }
    [/config/production.rb]
    config.action_mailer.default_url_options = { host: "www.example.com" }
    

    NOTE: If you are working inside a rails engine remember to do the same for your dummy app inside the engine tests: path_to_your_engine/test/dummy/config/environments/* because when you test the engine it's what rails is testing against.

    2.Add the host option to the foo_url method like this:

    listing_url(listing, host: request.host) # => 'http://localhost:3000/listings/1'
    

    3.Not output the host with the option :only_path to true.

    listing_url(listing, only_path: true ) # => '/listings/1'   
    

    IMHO I don't see the point on this one because in this case I would use the listing_path method

    0 讨论(0)
  • 2020-11-27 11:31

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

    In the developemnt.rb / test.rb, can be more concise as following:

    Rails.application.configure do
      # ... other config ...
    
      routes.default_url_options[:host] = 'localhost:3000'
    end
    
    0 讨论(0)
  • 2020-11-27 11:31

    The above answer did not work for me, at least not as I wanted. I realised config.action_mailer.default_url_options = { host: "localhost", port: 3000 } after installing devise. Hope it will help someone with the same problem.

    0 讨论(0)
  • 2020-11-27 11:33

    just in case someone finds this searching for errors concerning ActiveStorage:

    if you have a controller-action where you want to generate upload-urls etc with the local disc-service (most likely in test environment), you need to include ActiveStorage::SetCurrent in the controller in order to allow blob.service_url_for_direct_upload to work correctly.

    0 讨论(0)
  • 2020-11-27 11:33

    Adding the default_url in routes not the right solution although, it works for some cases.

    You've to set the default_url in each environment(development, test, production).

    You need make these changes.

        config/environments/development.rb
         config.action_mailer.default_url_options = 
          { :host => 'your-host-name' }  #if it is local then 'localhost:3000'
    
     config/environments/test.rb
          config.action_mailer.default_url_options = 
          { :host => 'your-host-name' }  #if it is local then 'localhost:3000'
    
      config/environments/development.rb
         config.action_mailer.default_url_options = 
          { :host => 'your-host-name' }  #if it is local then 'localhost:3000'
    
    0 讨论(0)
提交回复
热议问题