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

前端 未结 15 2646
深忆病人
深忆病人 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: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

提交回复
热议问题