15

I am trying to put a Rails link_to statement inside a Mailer email that includes the full-path (ie - http://localhost/contacts/id/confirm). The link_to statement that I am trying works in my standard View in /pages/options, but not in the Mailer email.

Here is my /pages/options Controller code:

class PagesController < ApplicationController
    def options
    end
end

And here's the pages/options View:

<div>
    <%= link_to "here", :controller => "contacts", :action => "confirm", 
    :only_path => false, :id => 17 %>
</div>

When I put this link into the following mailer (welcome_email.html.rb), I am getting the error below. Any help with this will be greatly appreciated.

<!DOCTYPE html>
<html>
<head>
    <meta content="text/html; charset=UTF-8" http-equiv="Content-Type" />
</head>
<body>
    <%= link_to "here", :controller => "contacts", :action => "confirm",
     :only_path => false, :id => 17 %>
</body>
</html>

The error message:

RuntimeError in Contacts#create

Showing C:/Documents and Settings/Corey Quillen/My Documents/Dev/Dev    
Projects/my_project
Project/my_project/app/views/user_mailer/welcome_email.html.erb where line #7  
raised:

Missing host to link to! Please provide :host parameter or set  
default_url_options[:host]
Extracted source (around line #7):

4:     <meta content="text/html; charset=UTF-8" http-equiv="Content-Type" />
5:   </head>
6:   <body>
7:     <%= link_to "here", :controller => "contacts", :action => "confirm", :only_path    
=> false, :id => 17 %>
8:   </body>
9: </html>
Corey Quillen
  • 1,566
  • 4
  • 24
  • 52

6 Answers6

21

First step:

#config/environments/production.rb
config.action_mailer.default_url_options = { :host => 'www.example.com' }

#config/environments/development.rb
config.action_mailer.default_url_options = { :host => 'localhost:3000' }

Second step:

<%= link_to "here", confirm_contacts_url(17) %>
zolter
  • 7,070
  • 3
  • 37
  • 51
  • 10
    Thanks zolter. Others, if this is still not working, one important but subtle thing to ensure is that you are using confirm_contacts_url() and not confirm_contacts_path(). _path always returns a relative url even if the host is set correctly in your config. – Kelvin Jul 23 '12 at 23:41
13

Because mailers aren't run inside the response stack, they have no idea what host they were called from: that's why you're running into this error. It's easy to fix, change the code to include the host:

<%= link_to "here", :controller => "contacts", :action => "confirm",
:only_path => false, :id => 17, :host => "example.com" %>

You can also set the default host on a per-application basis inside of your application.rb (or any of your environments) by specifying this:

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

For the full documentation on ActionMailer and why this problem occurs, check out the ActionMailer documentation.

Veraticus
  • 15,944
  • 3
  • 41
  • 45
2

Based on the current guides http://guides.rubyonrails.org/action_mailer_basics.html#generating-urls-in-action-mailer-views , I think the best way is to use the url_for and configuring a host in the enviroment config file. Or better yet to use a name route.

Example with named route

link_to @user.fullname, user_url(@user)
Victor Martins
  • 1,355
  • 1
  • 12
  • 23
1

You need to supply the :host option with the link_to.

You can also set the config.action_mailer.default_url_options in config/environments/*.rb files to appropriate settings so they are picked for link_to in all mailers

eg -

in config/environments/production.rb

config.action_mailer.default_url_options = { :host => 'www.example.com' }
abhishek
  • 998
  • 6
  • 9
0

If the full url always matches the request of the user, you can alternatively use the actionmailer-with-request gem to have the request be forwarded to action mailer, then you can reference the request within your mail template, like:

<%= link_to "log into the admin page", request.base_url + admin_root_path %> 
Peter DeWeese
  • 18,141
  • 8
  • 79
  • 101
0

I think you need to pass the host in a before filter when using it in an e-mail format. I recall using this page when I had a similar issue: http://www.cherpec.com/2009/06/missing-host-to-link-to-please-provide-host-parameter-or-set-default_url_optionshost/

Here is an SO post on it too with a different take

Community
  • 1
  • 1
ScottJShea
  • 7,041
  • 11
  • 44
  • 67