Ruby-on-rails 如何为 Rails ActionMailer 配置主机名?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/798917/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 21:11:16  来源:igfitidea点击:

How do I configure the hostname for Rails ActionMailer?

ruby-on-railsactionmailerhostnameactioncontroller

提问by edebill

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.

我正在处理一个相当传统的忘记密码电子邮件 - 我想通过电子邮件向用户发送一个嵌入在链接中的密码更改令牌,他们可以单击该令牌以更改其密码。我通过传统的 ActionMailer 发送电子邮件。

If I use a normal link_to tag

如果我使用普通的 link_to 标签

<%= 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?

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

回答by

default_url_optionsis available from config.action_mailerand should be set in your environment's configuration file.

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

For example, in config/environments/production.rb:

例如,在config/environments/production.rb

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

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

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

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

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:

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

回答by mcr

You probably want to set :protocol => 'https' as well, btw.

您可能还想设置 :protocol => 'https' ,顺便说一句。

config.action_mailer.default_url_options = { 
    :host => "portal.example.com", 
    :protocol => 'https' 
}

回答by Michael Reinsch

There is another alternative, as described in http://pivotallabs.com/how-i-leaned-to-stop-hating-and-love-action-mailer/

还有另一种选择,如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.

但是,如果您计划不通过控制器(例如从命令行或响应另一封电子邮件)发送电子邮件,则需要静态配置。

回答by Sameer

Setting default_url_optionsdirectly is deprecated in Rails 3.1. Use url_forinstead.

default_url_optionsRails 3.1 中不推荐直接设置。改用url_for

Add parameter :protocolto override default value (http), :protocol => 'https://'. This will create url starting with "https://..." instead of default "http://"

添加参数:protocol以覆盖默认值 (http), :protocol => 'https://'. 这将创建以“https://...”开头的网址,而不是默认的“http://”

回答by Zhenya

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:

有趣的是,我和你有同样的问题,但在单元测试中(同时遵循 Michael Hartl 的 railstutorial)。我的 test.rb 文件中有这一行,但这没有帮助:

config.action_mailer.default_url_options = { host: 'example.com', protocol: 'http' }

I've also added another line like this to test.rb, and surprisingly this solved the issue

我还在 test.rb 中添加了另一行这样的行,令人惊讶的是这解决了这个问题

default_url_options = { host: 'example.com', protocol: 'http' }

回答by Joe

Setting default_url_options directly is deprecated in Rails 3.1

Rails 3.1 中不推荐直接设置 default_url_options

Use the url_for helper to create it:

使用 url_for 助手来创建它:

<%= link_to "click here", url_for(:controller => foo, :action => 'bar', :token => token, :host => 'www.yourdomain.com') %>

回答by Rob Di Marco

Can you just do

你能做吗

<%="click here", :controller => foo, :action => 'bar', :token => token, :host=>request.host -%>