如何为Rails ActionMailer配置主机名? [英] How do I configure the hostname for Rails ActionMailer?

查看:96
本文介绍了如何为Rails ActionMailer配置主机名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一种相当传统的忘记密码电子邮件-我想通过电子邮件将隐藏在链接中的密码更改令牌发送给用户,用户可以单击该令牌来更改密码。我通过传统的ActionMailer发送电子邮件。

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.

如果我使用普通的link_to标签

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.

如果我添加

:only_path => false ,然后出现错误,提示我需要设置 default_url_options [:host] 。 ActionController文档暗示您可以通过覆盖控制器中的#default_url_options方法来做到这一点。肯定有一个配置选项可以告诉Rails的主机名是什么,而无需添加我自己的配置文件,对其进行解析等?

: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?

推荐答案

default_url_options 可从 config.action_mailer 获得,并且应在您环境的配置文件中进行设置。

default_url_options is available from config.action_mailer and should be set in your environment's configuration file.

例如,在 config / environments / production.rb

config.action_mailer.default_url_options = {
  :host => 'www.yourdomain.com'
}

对于本地测试,请修改 config / environments / development.rb

For local testing, modify config/environments/development.rb:

config.action_mailer.default_url_options = {
  :host => '127.0.0.1',
  :port => 3000
}

然后,假设您有一个名为的命名路由forgot_password_login ,您可以使用以下方式在邮件程序中生成登录链接URL:

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:

forgot_password_login_url(:token => 'a7s8q15sk2...')

这篇关于如何为Rails ActionMailer配置主机名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆