1
votes

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

3 Answers

9
votes

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" }
2
votes

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
votes

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