我正在尝试进行自定义身份验证,以更好地了解幕后发生的事情。花了一段时间让它接受密码和密码确认,但现在它不起作用,我完全没有想法
class UsersController < ApplicationController
def new
@user = User.new
end
def create
par = params[:user]
@user = User.new(params[:user])
if @user.verify_password_confirmation(par[:password], par[:password_confirmation]) && @user.save
sign_in @user
redirect_to user_url(@user)
else
render 'new'
end
end
end
在 attr_accessor 中有密码和密码确认有什么危险吗?
require 'bcrypt'
class User < ActiveRecord::Base
attr_accessor :password_confirmation, :password
attr_accessible :name, :email, :password, :password_confirmation
before_save { email.downcase! }
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d]\-.]+\.[a-z]+\z/i
validates :email, presence: true,
format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
validates :password, length: { minimum: 6 }
validates :password_confirmation, presence: true
def password=(password)
self.password_digest = BCrypt::Password.create(password)
end
def verify_password(password) #for the sessions, which obviously I can't check yet
BCrypt::Password.new(self.password_digest) == password
end
def verify_password_confirmation(pass, pass_con) #couldn't see how else to confirm it
pass_con == pass
end
def reset_session_token!
self.session_token = SecureRandom::base64(32)
self.save!
self.session_token
end
end
编辑:具体的问题是它在或者@user.save
重新verify_password_confirmation
渲染时都失败了