25

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?

Community
  • 1
  • 1
emersonthis
  • 32,822
  • 59
  • 210
  • 375

2 Answers2

39

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

dax
  • 10,779
  • 8
  • 51
  • 86
  • Where? In which file did you add it? – emersonthis Sep 11 '13 at 13:39
  • 2
    config/application.rb is the configuration file loaded for every kind of environment (dev, prod, test, etc). The others, like config/development.rb, are loaded with the corresponding environment. So it's up to you @Emerson, you can choose depending on your needs/environments – MrYoshiji Sep 11 '13 at 13:42
  • 3
    @dax Thanks. This worked. I'm curious to know more about what this is and why it works. Is it documented anywhere? – emersonthis Sep 11 '13 at 14:00
  • @MrYoshiji So if config/application.rb is the global settings for environments, what is config/environment.rb? <--n00b question – emersonthis Sep 11 '13 at 14:01
  • I found the answer [here](http://stackoverflow.com/q/16228731/2128691), but it's other places also - and it doesn't really include the 'why' unfortunately – dax Sep 11 '13 at 14:04
15

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'
Nicholas Barry
  • 558
  • 2
  • 5
  • 15
Prabhakar
  • 6,458
  • 2
  • 40
  • 51
  • Thank you. I only had this in `development.rb`; hadn't occurred to me that it had to be in `test.rb` Rails 5.0.2. Ruby 2.4. – Greg Mar 16 '17 at 19:33