我试图在包含完整路径(即-http://localhost/contacts/id/confirm)的Mailer电子邮件中放入Rails link_to语句。我正在尝试的link_to语句在/ pages / options的标准视图中有效,但在Mailer电子邮件中则无效。
这是我的/ 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>
最佳答案
因为邮件不在响应堆栈中运行,所以他们不知道从何处调用主机:这就是为什么您遇到此错误的原因。易于修复,更改代码以包含主机:
<%= 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 documentation。