How to include full-path in Rails 3 link_to statement?

不羁岁月 提交于 2019-12-30 16:27:04

问题


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>

回答1:


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.




回答2:


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) %>



回答3:


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)



回答4:


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' }



回答5:


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




回答6:


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 %> 


来源:https://stackoverflow.com/questions/9389874/how-to-include-full-path-in-rails-3-link-to-statement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!