1

i am using rails and devise and i am trying to send emails but i get the following error in production and test eviroments, in development all works fine:

ActionView::Template::Error: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true # ./app/views/devise/mailer/reset_password_instructions.html.erb:9:in `_app_views_devise_mailer_reset_password_instructions_html_erb___200197999_97517150'

My development.rb

config.action_mailer.delivery_method = :smtp
  config.action_mailer.default_url_options = { :host => "localhost:3000" }
  config.action_mailer.perform_deliveries = true
  config.action_mailer.raise_delivery_errors = true
  config.action_mailer.default :charset => "utf-8"

test.rb

config.action_mailer.delivery_method = :test
config.action_mailer.default_url_options = { :host => "localhost:3000" }

production.rb

  config.action_mailer.default_url_options = { :host => "mydomain.com" }
  config.action_mailer.delivery_method = :smtp
  config.action_mailer.perform_deliveries = true
  config.action_mailer.raise_delivery_errors = false
  config.action_mailer.default :charset => "utf-8"

reset_password_instructions.html.erb

<p>Hello, <%= @resource.full_name %>!</p>

<p>You have request a new passord</p>

    <p><%= link_to 'Change my password', edit_password_url(@resource, :reset_password_token => @token), class: 'btn btn-info' %></p>
    <p>If you didn't request this, please ignore this email.</p>
    <p>Your password won't change until you access the link above and create a new one.</p>

Where is the error?

3 Answers 3

10

I am not sure if it was a typo or not, but looks like the host definition for test and production is not set properly.

test.rb:

config.action_mailer.default_url_options = { :host => "http://loclahost:3000" }

production.rb:

config.action_mailer.default_url_options = { :host => "http:/mydomain.com" }

It should work once they are set properly. For example:

test.rb:

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

production.rb:

config.action_mailer.default_url_options = { :host => "mydomain.com" }
4
  • Hi, i changed my lines with yours but still have the same error it seems like there is a variable that it cant find , (i will edit my question and correct the urls) Nov 17, 2013 at 14:53
  • Was the server restarted? Also, can you describe where is the application deployed on production? Nov 17, 2013 at 21:39
  • Yes i restart the server, the application is deployed on heroku Nov 18, 2013 at 23:06
  • I found an intializer/setup_mailer.rb that containse the following line: ActionMailer::Base.default_url_options[:host] = ENV["DEFAULT_HOST"] that maybe override the code in production.rb in fact the env variable DEFAULT_HOST was not set on heroku and after setting it in procuction all works fine. Still having the same issue in the test enviroment by the way. Nov 18, 2013 at 23:18
2

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

Rails 4.1.x

Thanks to maudulus

# Default Mailer Host
  Rails.application.routes.default_url_options[:host] = 'domain.com'

*Note do not load the value from an ENV variable or it will throw the error.

1
  • 1
    Thanks for adding this note. I was loading value from Figaro and couldn't figured out why it was not working !
    – anthony
    Dec 4, 2016 at 10:22
1

I've solved the problem in this way: https://stackoverflow.com/a/9402816/1759437, setting the default_url_options on application_controller.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.