如何在 Rails 3 link_to 语句中包含完整路径?
我试图将 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
第一步:
第二步:
First step:
Second step:
因为邮件程序不在响应堆栈内运行,所以它们不知道从哪个主机调用它们:这就是您遇到此错误的原因。修复很容易,更改代码以包含主机:
您还可以通过指定以下内容,在 application.rb (或任何环境)内的每个应用程序的基础上设置默认主机:
有关 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:
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:
For the full documentation on ActionMailer and why this problem occurs, check out the ActionMailer documentation.
基于当前指南 http://guides.rubyonrails。 org/action_mailer_basics.html#generate-urls-in-action-mailer-views ,我认为最好的方法是使用 url_for 并在中配置主机环境配置文件。或者更好的是使用名称路由。
命名路由示例
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
提供:host
选项。您还可以将 config/environments/*.rb 文件中的
config.action_mailer.default_url_options
设置为适当的设置,以便在所有邮件程序中为 link_to 选择它们,例如 -
在 config/环境/生产.rb
You need to supply the
:host
option with thelink_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 mailerseg -
in config/environments/production.rb
我认为当以电子邮件格式使用它时,您需要在过滤器之前传递主机。我记得当我遇到类似问题时使用此页面: 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
如果完整的 url 始终与用户的请求匹配,您也可以使用
actionmailer-with-request
gem 将请求转发到 action mailer,然后您可以在邮件模板中引用该请求, 喜欢: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: