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它的主机名是什么,而不添加我自己的配置文件,解析它等?
7 个解决方案
#1
36
default_url_options
is available from config.action_mailer
and 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...')
#2
26
You probably want to set :protocol => 'https' as well, btw.
你可能想设置:protocol =>'https',顺便说一句。
config.action_mailer.default_url_options = {
:host => "portal.example.com",
:protocol => 'https'
}
#3
4
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.
但是,如果您计划在不通过控制器的情况下发送电子邮件(例如,从命令行或响应其他电子邮件),则需要静态配置。
#4
0
Setting default_url_options
directly is deprecated in Rails 3.1. Use url_for instead.
在Rails 3.1中不推荐直接设置default_url_options。请改用url_for。
Add parameter :protocol
to override default value (http), :protocol => 'https://'
. This will create url starting with "https://..." instead of default "http://"
添加参数:protocol以覆盖默认值(http),:protocol =>'https://'。这将创建以“https:// ...”开头的网址,而不是默认的“http://”
#5
0
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的轨道教程)。我在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' }
#6
-1
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') %>
#7
-2
Can you just do
你能做到吗?
<%="click here", :controller => foo, :action => 'bar', :token => token, :host=>request.host -%>
#1
36
default_url_options
is available from config.action_mailer
and 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...')
#2
26
You probably want to set :protocol => 'https' as well, btw.
你可能想设置:protocol =>'https',顺便说一句。
config.action_mailer.default_url_options = {
:host => "portal.example.com",
:protocol => 'https'
}
#3
4
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.
但是,如果您计划在不通过控制器的情况下发送电子邮件(例如,从命令行或响应其他电子邮件),则需要静态配置。
#4
0
Setting default_url_options
directly is deprecated in Rails 3.1. Use url_for instead.
在Rails 3.1中不推荐直接设置default_url_options。请改用url_for。
Add parameter :protocol
to override default value (http), :protocol => 'https://'
. This will create url starting with "https://..." instead of default "http://"
添加参数:protocol以覆盖默认值(http),:protocol =>'https://'。这将创建以“https:// ...”开头的网址,而不是默认的“http://”
#5
0
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的轨道教程)。我在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' }
#6
-1
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') %>
#7
-2
Can you just do
你能做到吗?
<%="click here", :controller => foo, :action => 'bar', :token => token, :host=>request.host -%>