How do I configure the hostname for Rails ActionMa

2019-02-03 06:12发布

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?

7条回答
再贱就再见
2楼-- · 2019-02-03 06:29

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.

查看更多
Explosion°爆炸
3楼-- · 2019-02-03 06:30

Setting default_url_options directly is deprecated in Rails 3.1

Use the url_for helper to create it:

<%= link_to "click here", url_for(:controller => foo, :action => 'bar', :token => token, :host => 'www.yourdomain.com') %>
查看更多
够拽才男人
4楼-- · 2019-02-03 06:33

Can you just do

<%="click here", :controller => foo, :action => 'bar', :token => token, :host=>request.host -%>
查看更多
等我变得足够好
5楼-- · 2019-02-03 06:39

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:

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

default_url_options = { host: 'example.com', protocol: 'http' }
查看更多
做自己的国王
6楼-- · 2019-02-03 06:48

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://"

查看更多
Fickle 薄情
7楼-- · 2019-02-03 06:51

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

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

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

For local testing, modify 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(:token => 'a7s8q15sk2...')
查看更多
登录 后发表回答