密码长度验证永远不会通过has_secure_password传递

时间:2014-07-15 20:42:13

标签: ruby-on-rails bcrypt-ruby

我正在尝试向password添加长度验证,但它始终会因“密码太短”而出错。

a = Artist.new(artist_name: 'Dylan', email: 'dylan@example.com', password: 'notshort')
p a.errors.full_messages  # ["Password is too short (minimum is 6 characters)"]
a = Artist.new(artist_name: 'Dylan', email: 'dylan@example.com', password: 'short')
p a.errors.full_messages  # ["Password is too short (minimum is 6 characters)"]

在模型中我有

has_secure_password
validates_length_of :password, minimum: 6

如果我将验证更改为

validates_length_of :password, minimum: 6, allow_blank: true

notshort密码通过,short也是如此。

2 个答案:

答案 0 :(得分:3)

我也在使用Bcrypt。这似乎对我有用:

has_secure_password
validates :password, length: { minimum: 6, maximum: 20 }, on: :create

答案 1 :(得分:0)

而不是validates_length_of,请尝试使用validates,如下所示:

validates :password, length: { minimum: 6 }