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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With