我正在尝试构建密码重置电子邮件。我正在使用Rails 3.2.16和Ruby 1.9.3p327,我已经阅读了很多这个问题的答案,并尝试了几乎所有的东西。我也经历了action mailer basics guide,据我所知,这应该可行,但它还不顺利。我会逐步指导我如何设置它。
首先,因为我想在开发过程中使用development.rb
注意: 我每次编辑development.rb文件时都重置了应用程序。
#this is all the action mailer settings i have defined in development.rb
config.action_mailer.raise_delivery_errors = true # Set to true so that errors would be visible.
config.action_mailer.perform_deliveries = true # I read about this possible fix on SO
config.action_mailer.default_url_options = {
host: "boogle.dev"
}
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "smtp.office365.com",
:port => 587,
:domain => "mpowered.co.za",
:user_name => "support@mpowered.co.za",
:password => "password",
:authentication => :login,
:enable_starttls_auto => true
}
我的通知者类继承自ActionMailer
class Notifier < ActionMailer::Base
default from: "Mpowered - BEEtoolkit <support@mpowered.co.za>"
def deliver_password_reset_email(user)
@edit_password_reset_url = edit_password_reset_url(user.perishable_token)
@name = user.name
mail(
subject: "Password Reset Instructions",
to: user.email,
date: Time.now,
content_type: "text/html")
end
end
在我的用户模型中,我设置了发送邮件的方法以及设置perishable_token
def deliver_password_reset_instructions!
reset_perishable_token!
Notifier.deliver_password_reset_email(self)
end
密码重置控制器设置如下:
class PasswordResetsController < ApplicationController
before_filter :require_no_user
before_filter :load_user_using_perishable_token, :only => [ :edit, :update ]
def new
end
def create
@user = User.find_by_email(params[:email])
if @user
@user.deliver_password_reset_instructions!
flash[:notice] = "Instructions to reset your password have been emailed to you"
render action: :new
else
flash.now[:error] = "No user was found with email address #{params[:email]}"
render action: :new
end
end
def edit
end
def update
@user.password = params[:password]
# Only if your are using password confirmation
@user.password_confirmation = params[:password]
# Use @user.save_without_session_maintenance instead if you
# don't want the user to be signed in automatically.
if @user.save
flash[:success] = "Your password was successfully updated"
redirect_to @user
else
render action: :edit
end
end
private
def load_user_using_perishable_token
@user = User.find_using_perishable_token(params[:id])
unless @user
flash[:error] = "We're sorry, but we could not locate your account"
redirect_to root_url
end
end
end
我在路线中添加了资源:
resources :password_resets, :only => [ :new, :create, :edit, :update ]
我的观点很简单: 在app / views / password_resets / new.haml.html
中%br
= form_tag password_resets_path, class: 'form-inline' do
%legend Forgotten Password
%p Enter your email address and instructions to reset your password will be emailed to you:
%span.span1
= label_tag :email, 'Email'
= text_field_tag :email
= submit_tag 'Reset my password', class: 'btn'
%br
因此,一旦您提交有效的电子邮件,这应该发送邮件。 然后,您应该会收到一封包含以下内容的电子邮件:app / views / notifier / password_reset_instructions.html.haml
%h1 Password Reset Instructions
%p
A request to reset your password has been made. If you did not make
this request, simply ignore this email. If you did make this
request, please follow the link below.
= link_to "Reset Password!", @edit_password_reset_url
该链接应该会将您带到一个表格,然后您可以保存新的密码和密码确认。
应用程序/视图/ password_resets / edit.html.haml
- if @user
= form_for @user, :url => password_reset_path, :method => :put do |f|
%legend Change My Password
%p Please select a new password for your account
.span8
= f.field :password, :field_type => :password_field, :label => "New password"
= f.field :password_confirmation, :field_type => :password_field
.clearfix
= f.submit "Update my password", class: 'btn'
- else
%h3 We couldn't identify your reset code
%p We're sorry, but we could not locate any accounts with reset codes that matched yours.
%p If you are having difficulties resetting your password, try copying and pasting the URL from your password reset email into your browser or restarting the password reset process.
您可以保存新密码,然后再次登录..这是我在应用程序中设置的。但每当我尝试通过跟随系统发送它,它说电子邮件已发送,但没有任何东西来。我还尝试在控制台中加载用户,然后运行u.deliver_password_reset_instructions!
我明白了:
但我的收件箱中仍然没有电子邮件。我目前已将通知程序中的电子邮件地址设置为我自己的个人电子邮件地址,因此无论请求何种有效的电子邮件地址,都应该发送电子邮件给我。
过去12小时我一直在打墙,不知道在哪里转弯。我希望我已经做了一个新鲜的眼睛可以抓住的球。
答案 0 :(得分:3)
您需要在调用此方式的Mailer方法时添加 .deliver
def deliver_password_reset_instructions!
reset_perishable_token!
Notifier.deliver_password_reset_email(self).deliver
end
希望这有帮助