24

Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true

I randomly get this error at time, generally restarting the server fixes the issue for a while, and then it shows up again. I have added config.action_mailer.default_url_options = "localhost:3000", in the development and test.rb files.

Also, I have used include Rails.application.routes.url_helpers in one module to get access to the routes, I read this could be the reason I get these errors but removing it will leave me with no access to the routes.
The module is for the datatables gem.

Raunak Joneja
  • 540
  • 1
  • 3
  • 13
  • What you ended up doing @raunak Joneja ? – damuz91 Feb 09 '17 at 21:59
  • Possible duplicate of [Missing host to link to! Please provide :host parameter or set default\_url\_options\[:host\]](https://stackoverflow.com/questions/7219732/missing-host-to-link-to-please-provide-host-parameter-or-set-default-url-optio) – Teemu Leisti May 29 '18 at 16:25

4 Answers4

42

For Rails 5, use:

Rails.application.routes.default_url_options[:host] = "XXX"

You can put this in a config/environments/ file (or any other initializer), or e.g. at the beginning of config/routes.rb.

Felix
  • 4,510
  • 2
  • 31
  • 46
David
  • 1,161
  • 10
  • 17
  • 1
    Where would you add that? In `config/environments/.rb`? – Felix Apr 27 '19 at 07:08
  • 1
    You can use it wherever you see fit. It can be the environment files. Usually, I end up with the same statement (reading the host value from an environment variable) and thus, I put this line at the top of `routes.rb` since it's the file defining/setting `Rails.application.routes`. – David Apr 29 '19 at 14:33
21

You should write in the following way

For Development(development.rb)

config.action_mailer.default_url_options = { :host => "localhost:3000" }

In production (production.rb)

 config.action_mailer.default_url_options = { :host => "myproductionsite.com" }
Rajarshi Das
  • 11,778
  • 6
  • 46
  • 74
  • 1
    didn't work, changed action_mailer to controller, made the value a hash, checked my heroku console, same error: 'ActionView::Template::Error (Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true):' prod host: { :host => "myherokuapp.herokuapp.com" } – Raunak Joneja Mar 22 '16 at 11:50
  • why do you change action_controller ? – Rajarshi Das Mar 22 '16 at 11:53
14

I had a similar error. The problem is in your configuration.

Try rewriting your configuration for your development.rb and test.rb like this:

config.action_mailer.default_url_options = { host: "localhost", port: 3000 }

Also check that your configuration in production.rb is written correctly like this:

config.action_mailer.default_url_options = { host: 'myherokuapp.herokuapp.com' }
Ruben Cruz
  • 201
  • 2
  • 5
7

You have updated the default url options for action mailer. URL helpers will take the option from action_controller settings.

config.action_controller.default_url_options = .....

BR

Andy
  • 135
  • 1
  • 1
  • 8
  • Also, url options is a hash, not a string. – Sergio Tulentsev Mar 21 '16 at 13:20
  • 1
    didn't work, changed action_mailer to controller, made the value a hash, checked my heroku console, same error: 'ActionView::Template::Error (Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true):' prod host: { :host => "myherokuapp.herokuapp.com" } – Raunak Joneja Mar 21 '16 at 15:45