Reputation: 1576
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>
Upvotes: 15
Views: 19437
Reputation: 18333
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 %>
Upvotes: 0
Reputation: 1383
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)
Upvotes: 2
Reputation: 7160
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) %>
Upvotes: 21
Reputation: 16064
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.
Upvotes: 13
Reputation: 7111
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
Upvotes: 0
Reputation: 1008
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' }
Upvotes: 1