ActionView::Template::Error: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true
Asked Answered
F

2

28

My ruby on rails action mailer runs all good in development environment, but in production environment, it keeps throw:

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

My development config is

config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
  :port           => xxx,
  :address        => 'smtp.example.org',
  :user_name      => '[email protected]',
  :password       => 'xxxxxxxx',
  :domain         => 'xxxxxx.example',
  :authentication => :plain,
}

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

My production config is

config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
  :port           => 587,
  :address        => 'smtp.example.org',
  :user_name      => '[email protected]',
  :password       => 'xxxxxx',
  :domain         => 'example.com',
  :authentication => :plain,
}
config.action_mailer.default_url_options = { :host => 'example.com' }

My other environments are:

ruby 2.1.1
rails 4.0.3
sidekiq
devise
devise-async

I have tried:

  1. Add the following in a initializer file

    ActionMailer::Base.default_url_options[:host] = "example.com"
    
  2. This answer here

Neither of them works.

Forereach answered 9/3, 2014 at 22:18 Comment(1)
This is strange. I just got the same error and adding config.action_mailer.default_url_options = { :host => "example.com" } to my application.rb resolved the issue... Are you sure your config is picked up correctly?Steamy
U
84

In the end I added the following with the correct value to each environment file:

test.rb / development.rb / production.rb

Devise 3.2.x - 4.x.x

Rails 4.1.x && Rails 4.2.x && Rails 5.x.x

Thanks to maudulus

# Default Mailer Host
  Rails.application.routes.default_url_options[:host] = 'domain.example'
Uproar answered 17/7, 2014 at 6:18 Comment(7)
Thanks for the solution. I was able to load from an ENV variable. Rails 4.1.7.Coprophagous
Awesome I'm glad that has been updated. Thanks for the comment.Uproar
Finally a solution that works, this question is a perfect example where most results on Stackoverflow are simply outdated or just bad.Addington
This was the correct answer for me for Rails 5.0.0 and Devise 4.2.0Boschvark
Thanks ! The only solution that worked in my case (Rails 5.0) !Freeliving
I don't get what the problem is at heart? What is a missing host? Why doesn't it infer the host?Iorgos
Finally, a solution that helped me that wasn't setting config.default_url_options = { :host => 'localhost' }Horten
J
4

Try restarting the server. Even in Rails 4.1 the config is not reparsed without a restart.

Joanne answered 7/7, 2014 at 17:55 Comment(1)
Alongside this, if you're triggering mail actions from the console, you might need to restart the console too.Moralize

© 2022 - 2024 — McMap. All rights reserved.