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
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
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' }
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)
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 %>
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.
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) %>