我试图在包含全路径(即 –
http://localhost/contacts/id/confirm)的邮件邮件中放置一个Rails link_to语句.我正在尝试的link_to语句在我的标准View in / 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",: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",:only_path
- => false,:id => 17 %>
- 8: </body>
- 9: </html>
解决方法
因为邮件程序没有在响应堆栈中运行,他们不知道他们被调用的主机:这就是为什么你遇到这个错误.很容易修复,更改代码以包含主机:
- <%= link_to "here",:id => 17,:host => "example.com" %>
您还可以通过指定以下方式在application.rb(或任何环境)中的每个应用程序的基础上设置默认主机:
- config.action_mailer.default_url_options = { :host => "example.com" }
有关ActionMailer的完整文档以及为什么会出现此问题,请查看ActionMailer documentation.