2

我正在为我的站点在 rails 和password_reset.text.erb我当前发送的邮件文件中创建密码重置功能

http://localhost:3000/password_resets/<%=@user.password_reset_token%>/edit/

在我的开发环境中。如果令牌与保存的模型匹配,这将点击我的控制器进行密码重置并重定向用户以编辑密码。

但是,我想动态配置它,所以当我部署到 heroku 时,它会知道将其更改为mywebsite.com/password_resets/...

我该怎么做?

编辑:

def password_reset(user)
    @user = user
    mail(to: user.email, subject: "Bitelist Password Reset")
end
4

1 回答 1

1

Typically I configure the host information for the mailer in an appropriate config/environment file.

config.action_mailer.default_url_options = { :host => "example.com" }

You can take a look at the "Generating URLs" section of this page: http://rails.rubyonrails.org/classes/ActionMailer/Base.html

With that configuration set typical routing structures seem to work quite nicely. I am not 100% positive what routes will be available in your situation so you may still need to construct the full URL somewhat manually to include the reset token component.

To generate the actual URLs you can potentially use named routes if your routes are set up in a way that you have a named route that takes a user token. i.e.

<%= edit_password_resets_url(@user.password_reset_token) %>

If you do not have the token integrated into an existing route you may need to manually build the URL:

<%= "#{url_for(:controller => 'password_resets', :action => 'whatever_action_this_is')}/#{@user.password_reset_token}/edit/" %>

or even build it completely manually using default_url_options[:host] and then add the rest that you have above.

If need be you could also set the host at request time although that may be overkill (and will not be thread safe).

def set_mailer_host
  ActionMailer::Base.default_url_options[:host] = request.host_with_port
end
于 2012-06-30T22:11:34.447 回答