Ruby-on-rails – Rails: Missing host to link to! Please provide :host parameter or set default_url_options[:host]

rspecruby-on-rails

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 config.action_mailer.default_url_options to solve this same bug elsewhere, but now I'm getting this error when trying to use a URL helper inside an RSpec spec. I have no idea where it's expecting default_url_options to be set.

 Failure/Error: listing_url(listing).should match(/\/\d+-\w+$/)
 RuntimeError:
   Missing host to link to! Please provide :host parameter or set default_url_options[:host]
 # ./spec/routing/listing_routing_spec.rb:9:in `block (3 levels) in <top (required)>'

This code has nothing to do with emails/ActionMailer, it just happens to need a URL instead of a path.

Any ideas?

Best Answer

You need to add the following line at every environment:

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

That way, it can work in all environments and could be different from environment to environment. For example:

development.rb

config.action_mailer.default_url_options = { :host => "dev.yourhost.com" }

test.rb

config.action_mailer.default_url_options = { :host => "test.yourhost.com" }

production.rb

config.action_mailer.default_url_options = { :host => "www.yourhost.com" }