emersonthis
emersonthis

Reputation: 33408

ActionView::Template::Error: Missing host to link to

As the title implies, I'm getting this error when I try to use link_to in my mailer templates:

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

I tried to follow it's instructions, and I also found this post, but I'm still getting the error.

I've tried adding config.action_mailer.default_url_options = { host: 'example.com' } to each of the following files and none of them worked:

/config/environment.rb
/config/application.rb (inside the module for my app)
/config/development.rb (I'm in development mode)

Has something changed in Rails 4 that makes this work differently?

Upvotes: 25

Views: 11996

Answers (2)

I am PK
I am PK

Reputation: 6774

You've to set the default_url in each environment(development, test, production).

You need make these changes.

    config/environments/development.rb
     config.action_mailer.default_url_options = 
      { :host => 'your-host-name' }  #if it is local then 'localhost:3000'

 config/environments/test.rb
      config.action_mailer.default_url_options = 
      { :host => 'your-host-name' }  #if it is local then 'localhost:3000'

  config/environments/production.rb
     config.action_mailer.default_url_options = 
      { :host => 'your-host-name' }  #if it is local then 'localhost:3000'

Upvotes: 15

dax
dax

Reputation: 11017

I ran into this problem the other day and this is what ended up working for me:

Rails.application.routes.default_url_options[:host] = '???'

edit

This goes in each environment file - development.rb, test.rb and production.rb (and more if you have them) with the corresponding host name in each one

Upvotes: 39

Related Questions