Rails 设计自定义身份验证给出错误未定义的方法 `valid_password?'

Rails devise custom authentication give error undefined method `valid_password?'

我在 rails 应用程序中使用 devise 进行身份验证,我需要使用自定义检查密码,我为此使用了

 def login
    @email = params[:email]
    @pass = params[:pass]
    @doctor = Doctor.find_by_email(@email).valid_password?(@pass)  
    respond_with(@doctor)
  end

但是我遇到了以下错误。

undefined method `valid_password?' for nil:NilClass

试试这个对你有用的

def login
    @email = params[:email]
    @pass = params[:pass]
    @doctor = Doctor.find_by_email(@email)
    if @doctor.nil?
      respond_with('User not available')
    else
      if(@doctor.valid_password?(@pass))
       respond_with(@doctor)
      else
        respond_with('wrong password')
      end  
   end
end