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?
The files under config/ are not automatically reloaded in development. If you have made changes to this file, ensure that you have restarted the Rails server, and any Rails console sessions for the changes to take effect.
Setting it dynamically solved the problem.
# application_controller.rb
before_filter :mailer_set_url_options
...
def mailer_set_url_options
ActionMailer::Base.default_url_options[:host] = request.host_with_port
end
I realize this configuration doesn't belong in a controller by convention, but for development purposes it seems to suffice. I can always code it into into the production environment configuration.
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"}
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.