新手问题 has_secure_password 关于 password_confirmation 的疑惑

vincent178 · 2013年02月18日 · 最后由 Vincent178 回复于 2013年02月18日 · 7642 次阅读

各位大大,麻烦看看。 User.rb

class User < ActiveRecord::Base
  validates :password, presence: true,
                       length: { minimum: 6 }
  has_secure_password
end

user_spec.rb

describe "when password confirmation is nil" do
  before { @user.password_confirmation = nil }
  it { should_not be_valid }
end

测试不通过,直到在 user.rb 上加上

validates :password_confirmation, presence: true

在 has_secure_password 的源代码里有validates_confirmation_of :password,还需要加 password_confirmation 的验证吗? 求解。

第一确保 model 里加入了 validates_confirmation_of :password

password_confirmation 要和 password 一起提交,password_confirmation 才有用。一般都不会验证 password_confirmation 的。

#1 楼 @metal 我有一个疑问,has_secure_password 的源代码里已经有 validates_confirmation_of 了,在 model 里应该不用再加了吧。 http://api.rubyonrails.org/classes/ActiveModel/SecurePassword/ClassMethods.html

#2 楼 @Vincent178 你点一下souce show看看。

def has_secure_password
  # Load bcrypt-ruby only when has_secure_password is used.
  # This is to avoid ActiveModel (and by extension the entire framework) being dependent on a binary library.
  gem 'bcrypt-ruby', '~> 3.0.0'
  require 'bcrypt'

  attr_reader :password

  validates_confirmation_of :password
  validates_presence_of     :password_digest

  include InstanceMethodsOnActivation

  if respond_to?(:attributes_protected_by_default)
    def self.attributes_protected_by_default
      super + ['password_digest']
    end
  end
end

我这几天也遇到同样的问题,后来我发现是 password 要和 password_confirmation 同时使用的,原因也是在上面,因为 validates_confirmation_of :password 只是验证了 password。

user = User.new(:name => "metal", :password => '123456', :password_confirmation => nil)
user.valid? # true

user = User.new(:name => "metal", :password => '123456', :password_confirmation => '')
user.valid? false
user.errors.messages # {:password=>["两次输入的密码不相同"]}

可以只在 password 不为空的时候验证 password_confirmation presence

#3 楼 @metal 嗯,正如你所说的,要使用 validates_confirmation_of :password 需要添加 password_confirmation 的 presence 验证,不然,confirmation 信息是 nil 都是可以通过验证的。是我原来用的时候不仔细。

需要 登录 后方可回复, 如果你还没有账号请 注册新账号