Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Missing host to link to!" for Devise

I installed Devise and things seem to stop up whenever I try to create an account.

The full error reads:

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

I've got the following code sitting in inside the development.rb block. I've tried it with and without the added smtp configurations.

config.action_mailer.default_url_options = { :host => 'localhost' }  
config.action_mailer.delivery_method = :smtp
...

Have I defined host incorrectly?

like image 205
fny Avatar asked Jul 10 '11 03:07

fny


2 Answers

Inherit your Application's default_url_options from ActionMailer.

By default, the default_url_options that you set for config.action_mailer in your environment files (development.rb, production.rb, etc.) do not get used as the default_url_options of your Application as a whole.

$ MyApp::Application.config.action_mailer.default_url_options
#=> {:host=>"lvh.me", :port=>"3000"}

$ MyApp::Application.default_url_options
#=> {}

So, when you try and use anything that requires the host and port (e.g. url) outside of the mailer, it doesn't know what to do.

You can solve this by hard coding the host and port in multiple places for the same environment, but you don't want to do that unless your ActionMailer actually uses a different host and port than the rest of your Application.

To solve this (and keep things as DRY as possible), you can automatically use the config.action_mailer.default_url_options as your entire Application's default_url_options.

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"}
like image 137
Joshua Pinter Avatar answered Sep 18 '22 11:09

Joshua Pinter


Add to the end of fileconfig/environment.rb line:

Rails.application.routes.default_url_options[:host] = 'yourhost.com'

It is not only problem in action_mailer, but also setting default host for the routes as well.

like image 42
Aleks Avatar answered Sep 19 '22 11:09

Aleks