gpt4 book ai didi

ruby-on-rails - 如何将 has_secure_password 与 field_with_errors 一起使用

转载 作者:数据小太阳 更新时间:2023-10-29 07:21:13 29 4
gpt4 key购买 nike

我正在使用 has_secure_password 来验证我的用户密码及其确认。我遇到的问题是,当出现任何错误时,字段不会被 field_with_errors div 包裹。我知道我可以添加

validates_presence_of :password, :on => :create
validates_presence_of :password_confirmation, :on => :create

但这会产生以下错误消息:

Password digest can't be blank.
Password can't be blank.
Password confirmation can't be blank

我想或者让 has_secure_password 用 field_with_errors div 包装有错误的字段或者删除“密码摘要不能为空”。完全错误。

谢谢。

最佳答案

SecurePassword具有此功能的模块非常简单,值得一看。好消息是在the master branch (Rails 4)它执行 validates_presence_of :password, :on => :create 这将解决您的问题,但与此同时您可能想自己模仿 User 模型上的 has_secure_password 方法。

class User < ActiveRecord::Base
attr_reader :password
attr_accessible :password # ...
validates_confirmation_of :password
validates_presence_of :password, on: :create
include ActiveModel::SecurePassword::InstanceMethodsOnActivation
end

还要确保 bcrypt 已加载到 Gemfile 中。

gem 'bcrypt-ruby', '~> 3.0.0', require: 'bcrypt'

希望对您有所帮助。

关于ruby-on-rails - 如何将 has_secure_password 与 field_with_errors 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10905870/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com