Cor*_*len 15 ruby-on-rails ruby-on-rails-3
我试图在包含完整路径的Mailer电子邮件中放置一个Rails link_to语句(即 - http:// localhost/contacts/id/confirm).我正在尝试的link_to语句在我的标准View in/pages/options中工作,但不在Mailer电子邮件中.
这是我的/ pages/options控制器代码:
class PagesController < ApplicationController
def options
end
end
Run Code Online (Sandbox Code Playgroud)
这是页面/选项查看:
<div>
<%= link_to "here", :controller => "contacts", :action => "confirm",
:only_path => false, :id => 17 %>
</div>
Run Code Online (Sandbox Code Playgroud)
当我将此链接放入以下邮件程序(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>
Run Code Online (Sandbox Code Playgroud)
错误消息:
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>
Run Code Online (Sandbox Code Playgroud)
zol*_*ter 19
第一步:
#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' }
Run Code Online (Sandbox Code Playgroud)
第二步:
<%= link_to "here", confirm_contacts_url(17) %>
Run Code Online (Sandbox Code Playgroud)
Ver*_*cus 13
因为邮件程序不在响应堆栈内运行,所以他们不知道从哪个主机调用它们:这就是你遇到这个错误的原因.它很容易修复,更改代码以包含主机:
<%= link_to "here", :controller => "contacts", :action => "confirm",
:only_path => false, :id => 17, :host => "example.com" %>
Run Code Online (Sandbox Code Playgroud)
您还可以通过指定以下内容,在application.rb(或任何环境)内基于每个应用程序设置默认主机:
config.action_mailer.default_url_options = { :host => "example.com" }
Run Code Online (Sandbox Code Playgroud)
有关ActionMailer的完整文档以及出现此问题的原因,请查看ActionMailer文档.
归档时间: |
|
查看次数: |
18480 次 |
最近记录: |