我正在尝试向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
也是如此。
答案 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 }