我正在设置重置密码功能。提交后,将发送一封电子邮件,其中包含重置密码链接。如railscast 第 274 集所述,我正在从头开始执行此操作。但是,当我提交电子邮件时,我收到此错误。
PasswordResetsController#create 中的 NoMethodError
undefined method `password_reset_sent_at=' for # <User:0xa9fd828>
app/models/user.rb:26:in `send_password_reset'
app/controllers/password_resets_controller.rb:7:in `create'
要求
参数:
{"utf8"=>"✓",
"authenticity_token"=>"HnuB/J15XZtz3MTCd25MCwm06M3G2AbcRH+cFxzdj+8=",
"email"=>"cauterise@gmail.com",
"commit"=>"Reset Password"}
开发日志说的和上面一样^
应用程序/模型/user.rb
def send_password_reset
generate_token(:password_reset_token)
self.password_reset_sent_at = Time.zone.now
save!
UserMailer.password_reset(self).deliver
end
def generate_token(column)
begin
self[column] = SecureRandom.urlsafe_base64
end while User.exists?(column => self[column])
end
应用程序/控制器/password_resets_controller.rb
class PasswordResetsController < ApplicationController
def new
end
def create
user = User.find_by_email(params[:email])
user.send_password_reset if user
redirect_to root_url, :notice => "Email sent with password reset instructions."
end
def edit
@user = User.find_by_password_reset_token!(params[:id])
end
def update
@user = User.find_by_password_reset_token!(params[:id])
if @user.password_reset_sent_at < 2.hours.ago
redirect_to new_password_reset_path, :alert => "Password reset has expired."
elsif @user.update_attributes(params[:user])
redirect_to root_url, :notice => "Password has been reset!"
else
render :edit
end
end
end
应用程序/控制器/application_controller.rb
def current_user
@current_user ||= User.find_by_auth_token!(cookies[:auth_token]) if cookies[:auth_token]
应用程序/vies/password_resets/new,html.haml
%h1 Reset Password
= form_tag password_resets_path, :method => :post do
.field
= label_tag :email
= text_field_tag :email, params[:email]
.actions
= submit_tag "Reset Password"
end
我已经做了 rake:db:reset 和 rake:db:migrate AND RESET SERVER。
编辑:数据库用户
id 整数主键
电子邮件 varchar(255)
密码哈希 varchar(255)
password_salt varchar(255)
created_at 日期时间
更新日期时间
auth_token varchar(255)
password_reset_token varchar(255)
password_reset_send_at 日期时间