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>
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) %>
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.
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)
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' }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With