我正在调试this problem with devise - 尽管我付出了最大的努力,但在更新用户而不提供密码时,我仍然会收到“密码不能为空”的错误。我已经尝试了官方解决方案,以及this SO question中建议的解决方案,但我仍然收到错误,尽管确保传递给update_attributes的opts不包含password
或password_confirmation
。
我正在试图弄清楚如何调试这个 - 我正在设计,activerecord,activemodel等等,但我似乎无法在正确的位置粘贴调试语句。
当我运行User.validators
时,我得到以下输出:
=> [#<ActiveRecord::Validations::PresenceValidator:0x007ff513507890 @attributes=[:email], @options={:if=>:email_required?}>,
#<ActiveRecord::Validations::UniquenessValidator:0x007ff51350fef0
@attributes=[:email],
@klass=
User(id: integer, name: string, email: string, encrypted_password: string, reset_password_token: string, reset_password_sent_at: datetime, remember_created_at: datetime, sign_in_count: integer, current_sign_in_at: datetime, last_sign_in_at: datetime, current_sign_in_ip: string, last_sign_in_ip: string, created_at: datetime, updated_at: datetime, school_id: integer, invitation_token: string, invitation_created_at: datetime, invitation_sent_at: datetime, invitation_accepted_at: datetime, invitation_limit: integer, invited_by_id: integer, invited_by_type: string, type: string, opt_in: boolean),
@options={:case_sensitive=>true, :allow_blank=>true, :if=>:email_changed?}>,
#<ActiveModel::Validations::FormatValidator:0x007ff51351c560
@attributes=[:email],
@options={:with=>/\A[^@\s]+@([^@\s]+\.)+[^@\s]+\z/, :allow_blank=>true, :if=>:email_changed?}>,
#<ActiveRecord::Validations::PresenceValidator:0x007ff5148e4b60 @attributes=[:password], @options={:if=>:password_required?}>,
#<ActiveModel::Validations::ConfirmationValidator:0x007ff50efb85a0 @attributes=[:password], @options={:if=>:password_required?}>,
#<ActiveModel::Validations::LengthValidator:0x007ff5150e91d0
@attributes=[:password],
@options={:allow_blank=>true, :minimum=>8, :maximum=>128}>,
#<ActiveRecord::Validations::PresenceValidator:0x007ff515125a90 @attributes=[:name], @options={}>]
设计本身(理论上)足够聪明,如果你没有改变它就不要求密码:
validates_presence_of :password, if: :password_required?
validates_confirmation_of :password, if: :password_required?
# ...
def password_required?
!persisted? || !password.nil? || !password_confirmation.nil?
end
您可以看到这反映在上面的User.validators
检查中。
但是,我仍然收到此错误!
如何追踪这个错误被添加到对象的位置,以及谁插入了有问题的验证器?
答案 0 :(得分:0)
您是否在注册控制器中添加了以下代码?
class RegistrationsController < Devise::RegistrationsController
protected
def update_resource(resource, params)
resource.update_without_password(params)
end
end
routes.rb中的路线
devise_for :users, controllers: {registrations: 'registrations'}