如何在 Rails 3 link_to 语句中包含完整路径?

发布于 2025-01-07 21:35:30 字数 1614 浏览 3 评论 0原文

我试图将 Rails link_to 语句放入包含完整路径的 Mailer 电子邮件中(即 - http://localhost/contacts/id/确认)。我尝试的 link_to 语句在 /pages/options 中的标准视图中有效,但在邮件程序电子邮件中无效。

这是我的 /pages/options 控制器代码:

class PagesController < ApplicationController
    def options
    end
end

这是页面/选项视图:

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

当我将此链接放入以下邮件程序 (welcome_email.html.rb) 时,我收到以下错误。对此的任何帮助将不胜感激。

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

错误信息:

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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

别再吹冷风 2025-01-14 21:35:30

第一步:

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

第二步:

<%= link_to "here", confirm_contacts_url(17) %>

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) %>
昔梦 2025-01-14 21:35:30

因为邮件程序不在响应堆栈内运行,所以它们不知道从哪个主机调用它们:这就是您遇到此错误的原因。修复很容易,更改代码以包含主机:

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

您还可以通过指定以下内容,在 application.rb (或任何环境)内的每个应用程序的基础上设置默认主机:

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

有关 ActionMailer 的完整文档和为什么会出现此问题,请查看 ActionMailer 文档

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.

错爱 2025-01-14 21:35:30

基于当前指南 http://guides.rubyonrails。 org/action_mailer_basics.html#generate-urls-in-action-mailer-views ,我认为最好的方法是使用 url_for 并在中配置主机环境配置文件。或者更好的是使用名称路由。

命名路由示例

link_to @user.fullname, user_url(@user)

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)
梅倚清风 2025-01-14 21:35:30

您需要为 link_to 提供 :host 选项。

您还可以将 config/environments/*.rb 文件中的 config.action_mailer.default_url_options 设置为适当的设置,以便在所有邮件程序中为 link_to 选择它们,

例如 -

在 config/环境/生产.rb

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

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' }
鸠书 2025-01-14 21:35:30

我认为当以电子邮件格式使用它时,您需要在过滤器之前传递主机。我记得当我遇到类似问题时使用此页面: http://www.cherpec.com/2009/06/missing-host-to-link-to-please-provide-host-parameter-or-set-default_url_optionshost/

这是一个SO 帖子也有不同的看法

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

倒带 2025-01-14 21:35:30

如果完整的 url 始终与用户的请求匹配,您也可以使用 actionmailer-with-request gem 将请求转发到 action mailer,然后您可以在邮件模板中引用该请求, 喜欢:

<%= link_to "log into the admin page", request.base_url + admin_root_path %> 

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 %> 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文