我试图在我的应用程序中绕过用户模型的密码验证。我有以下基于一些类似的 SO 问题:
模型.rb
attr_accessor :skip_password_validation
has_secure_password
validates :password, length: { minimum: 6 }, allow_nil: true, unless: :skip_password_validation
Controller .rb
@mem.skip_password_validation = true
if @mem.valid?
#do some stuff
else
render 'last_template'
end
虽然这始终呈现最后一个模板并返回密码验证错误。感谢您的帮助。
最佳答案
问题是 as mentioned in the docs has_secure_password
添加自己的验证:
The following validations are added automatically:
- Password must be present on creation
- Password length should be less than or equal to 72 characters
- Confirmation of password (using a password_confirmation attribute)
更新
Also as mentioned in the docs您可以通过提供 :validations
选项来禁用验证,将其设置为 false
:
For further customizability, it is possible to supress the default validations by passing
validations: false
as an argument.
关于ruby-on-rails - Rails 跳过条件验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30466053/