PasswordResets中的ActiveRecord :: RecordNotFound - 如何获取密码重置令牌以便正确保存?

时间:2016-04-14 21:08:16

标签: ruby-on-rails ruby-on-rails-4 rails-activerecord

我试图创建忘记密码功能,但我遇到了一个错误,即我的密码重置控制器无法识别密码重置令牌,尽管它已在电子邮件中发送。

忘记密码查看

= form_tag password_resets_path, :method => :post do
  .field
    = label_tag :email
    = text_field_tag :email, params[:email]
  .actions
    = submit_tag "Reset Password"

密码重置控制器

def create
  user = User.find_by_email(params[:email])
  user.send_password_reset if user
  redirect_to new_password_reset_path, :notice => "Email Sent"
end
def edit
  @user = User.find_by_password_reset_token!(params[:id])
end

用户模型

def send_password_reset
  self.password_reset_token = SecureRandom.urlsafe_base64.to_s
  self.password_reset_sent_at = Time.zone.now
  save!(:validate => false)
  UserMailer.password_reset(self).deliver
end

我在电子邮件中收到的链接如下

localhost:3000/password_resets/RrIf2WZB_C7iC3sCGNQHqg/edit

一切似乎都运转正常,我发送的电子邮件中包含上述链接,但是当我点击它时,我收到以下错误。

ActiveRecord::RecordNotFound in PasswordResets
Couldn't find User

我对如何生成和发送令牌而没有保存错误感到困惑,但却无法在数据库中找到它。我可能很傻,忽略了什么,但我无法理解。

佣金路线的结果| grep password_reset

      password_resets GET                   /password_resets(.:format)                password_resets#index
                      POST                  /password_resets(.:format)                password_resets#create
   new_password_reset GET                   /password_resets/new(.:format)            password_resets#new
  edit_password_reset GET                   /password_resets/:id/edit(.:format)       password_resets#edit
       password_reset GET                   /password_resets/:id(.:format)            password_resets#show
                      PATCH                 /password_resets/:id(.:format)            password_resets#update
                      PUT                   /password_resets/:id(.:format)            password_resets#update
                      DELETE                /password_resets/:id(.:format)            password_resets#destroy

UserMailer PasswordReset:

def password_reset(user)
    @user = user
    mail(:to => "#{user.username} <#{user.email}>", :subject => "Password Reset")
end

0 个答案:

没有答案