如何配置 Rails ActionMailer 的主机名?
我正在处理一个相当传统的忘记密码电子邮件 - 我想通过电子邮件向用户发送嵌入在链接中的密码更改令牌,他们可以单击该链接来更改密码。 我通过传统的 ActionMailer 发送电子邮件。
如果我使用普通的 link_to 标签,
<%= link_to "click here", :controller => foo, :action => 'bar', :token => token %>
我会得到一个相对链接 - 从电子邮件中毫无用处。
如果我添加
:only_path =>; false
,则错误提示我需要设置 default_url_options[:host]
。 ActionController 文档暗示您可以通过重写控制器中的 #default_url_options 方法来实现这一点。 当然有一个配置选项可以告诉 Rails 它的主机名是什么,而无需添加我自己的配置文件、解析它等?
I'm working on a fairly traditional forgot password email - I want to email the user a password change token embedded in a link that they can click on in order to change their password. I'm emailing via the traditional ActionMailer.
If I use a normal link_to tag
<%= link_to "click here", :controller => foo, :action => 'bar', :token => token %>
I get a relative link - rather useless from an email.
If I add in
:only_path => false
, then it errors saying I need to set default_url_options[:host]
. The ActionController docs imply that you do that by overriding the #default_url_options methods in your controller. Surely there's a configuration option to tell Rails what it's hostname is without adding my own config file, parsing it, etc?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
default_url_options
可从config.action_mailer
获取,并且应在您的环境的配置文件中进行设置。例如,在
config/environments/development.rb
中:对于本地测试,修改
config/environments/development.rb
:然后,假设您有一个名为
的命名路由>forgot_password_login
,您可以使用类似以下内容在邮件程序中生成登录链接 URL:default_url_options
is available fromconfig.action_mailer
and should be set in your environment's configuration file.For example, in
config/environments/production.rb
:For local testing, modify
config/environments/development.rb
:Then, assuming you have a named route called
forgot_password_login
, you can generate the login link URL in your mailer using something like this:您可能想要设置 :protocol => 顺便说一句,还有“https”。
You probably want to set :protocol => 'https' as well, btw.
还有另一种选择,如 http 中所述://pivotallabs.com/how-i-leaned-to-stop-hating-and-love-action-mailer/
该解决方案的优点是不需要任何配置(因此不那么麻烦),只要您从控制器内发送电子邮件,就可以正常工作。
但是,如果您计划在不通过控制器的情况下发送电子邮件(例如从命令行或响应另一封电子邮件),则需要静态配置。
There is another alternative, as described in http://pivotallabs.com/how-i-leaned-to-stop-hating-and-love-action-mailer/
This solution has the advantage that it doesn't require any configuration (so less of a hassle), and works fine as long as you send emails from within controllers.
But if you plan on sending email without going through a controller (e.g. from command line or in response to another email), you need the static configuration.
Rails 3.1 中不推荐直接设置
default_url_options
。 请改用 url_for 。添加参数
:protocol
覆盖默认值(http),:protocol => 'https://'
。 这将创建以“https://...”而不是默认的“http://”开头的网址Setting
default_url_options
directly is deprecated in Rails 3.1. Use url_for instead.Add parameter
:protocol
to override default value (http),:protocol => 'https://'
. This will create url starting with "https://..." instead of default "http://"有趣的是,我遇到了和你一样的问题,但是是在单元测试中(同时遵循迈克尔·哈特尔的railstutorial)。 我的 test.rb 文件中有这一行,但这没有帮助:
我还在 test.rb 中添加了另一行这样的行,令人惊讶的是,这解决了问题
Interestingly, I had the same issue as you did, but in unit tests (while following Michael Hartl's railstutorial). I had this line in my test.rb file, but that didn't help:
I've also added another line like this to test.rb, and surprisingly this solved the issue
Rails 3.1 中不推荐直接设置 default_url_options
使用 url_for 帮助器创建它:
Setting default_url_options directly is deprecated in Rails 3.1
Use the url_for helper to create it:
你能做吗
Can you just do