Missing host to link to! Please provide :host parameter or set default_url_options[:host]

Problem:
Missing host to link to! Please provide :host parameter or set default_url_options[:host] when sending emails.

Solution:
You can pass host parameter to url functions, but it’s cleaner to configure it with a before_filter globally in your application_controller.rb:

  # application_controller.rb
  before_filter :mailer_set_url_options
 
  ...
 
  def mailer_set_url_options
    ActionMailer::Base.default_url_options[:host] = request.host_with_port
  end

5 comments ↓

#1 Lau on 11.29.09 at 8:27 am

Thanks !! :-)
solved my problem.

#2 Sandy on 02.02.10 at 2:36 am

Or put it in environment.rb

config.action_mailer.default_url_options = { :host => “example.com” }

#3 Phillip on 04.23.10 at 5:19 pm

@Sandy,

While that would work, the before_filter approach has the added benefit of being dynamic to the environment Rails is currently running in. This gives you the flexibility to have links in development point to development, not production.

#4 Francesco on 04.24.10 at 8:10 am

Hello,

thanks for the infos, you solved my issue, now I’m using this in my application.rb and it works well for my needs.

config.action_mailer.default_url_options = { :host => Rails.env == “production” ? ‘domain.com’ : ‘localhost:3000′ }

#5 Keikun17 on 08.31.10 at 7:01 am

Francesco, it’s cleaner if you just let the environtment configs handle the default_url_options

Leave a Comment