Ruby on rails 未定义的方法';哈希密码';基于rails的敏捷Web开发

Ruby on rails 未定义的方法';哈希密码';基于rails的敏捷Web开发,ruby-on-rails,login,agile,Ruby On Rails,Login,Agile,我正在关注敏捷Web开发的第13次迭代,用于用户登录 我创建了一个迁移,将两列添加到我的用户模型中:hashd_password和salt 用户作品的创作 由于此错误,登录失败方法“authenticate”中未定义的方法“hashed_password” 问题在于: 在rails控制台中,我可以获取User.first.hashed_密码,似乎可以:-) 我输出了我接收的用户,它不是零 我试图像在rails控制台中那样输出user.hashd_密码,但这始终会引发相同的错误: NoMe

我正在关注敏捷Web开发的第13次迭代,用于用户登录

我创建了一个迁移,将两列添加到我的用户模型中:hashd_password和salt

  • 用户作品的创作
  • 由于此错误,登录失败方法“authenticate”中未定义的方法“hashed_password”
问题在于:

  • 在rails控制台中,我可以获取User.first.hashed_密码,似乎可以:-)
  • 我输出了我接收的用户,它不是零
  • 我试图像在rails控制台中那样输出user.hashd_密码,但这始终会引发相同的错误:
NoMethodError(用于#的未定义方法
hash_密码):
app/models/user.rb:21:in
authenticate' app/controllers/sessions\u controller.rb:6:in'create'

这是我的用户模型:

require 'digest/sha2'

class User < ActiveRecord::Base
has_and_belongs_to_many :products
has_many :created_products, :class_name => "Product", :foreign_key => :product_id

default_scope :order => "username ASC"


# Attributs pour le login (Livre)
validates :username, :presence => true, :uniqueness => true
validates :password, :confirmation => true
attr_accessor :password_confirmation
attr_reader :password
validate :password_must_be_present

def User.authenticate(name, password)
    logger.debug "---------- Beginning of Authenticate"
    if user = User.where(:username => name)

      logger.debug "utilisateur = #{user.inspect}"   # THIS IS OK AND NOT NIL
      logger.debug "utilisateur hashed PW = #{user.hashed_password}" # ERROR


        if user.hashed_password == encrypt_password(password, user.salt)
            return user
        end
    end
end

def User.encrypt_password(password, salt)
    Digest::SHA2.hexdigest(password + "wibble" + salt)
end

def password=(password)
    @password = password
    if (password.present?)
        generate_salt
        self.hashed_password = self.class.encrypt_password(password, salt)
    end
end


private

    def password_must_be_present
        errors.add(:password, "Mot de passe manquant") unless hashed_password.present?
    end

    def generate_salt
        self.salt = self.object_id.to_s + rand.to_s
    end

end
需要“摘要/sha2”
类用户“产品”,:外键=>:产品id
默认范围:order=>“用户名ASC”
#登录属性(Livre)
验证:username,:presence=>true,:university=>true
验证:密码,:确认=>true
属性访问器:密码确认
属性读取器:密码
验证:密码必须存在
def用户身份验证(名称、密码)
logger.debug“------------开始验证”
如果user=user.where(:username=>name)
logger.debug“利用率=#{user.inspect}”#这是正常的,不是零
logger.debug“Usilizateur hashed PW=#{user.hashed_password}”错误
如果user.hashed_password==加密_password(password,user.salt)
返回用户
终止
终止
终止
def User.ENCRYPTION_密码(密码,salt)
摘要::SHA2.hexdigest(密码+wibble+salt)
终止
def密码=(密码)
@密码=密码
如果(密码是否存在?)
生盐
self.hashed_password=self.class.encrypt_password(密码,salt)
终止
终止
私有的
def密码必须存在
错误。是否添加(:password,“Mot de passe manquant”),除非哈希值为\u password.present?
终止
def生成尿素盐
self.salt=self.object\u id.to\u s+rand.to\u s
终止
终止
User.where(:username=>name)返回一个ActiveRecord::Relation对象(因此您会看到错误消息)。尝试将if语句更改为:

if user = User.where(:username => name).first
这将设置第一个匹配的用户,这将是用户的一个实例,因此将有一个哈希_密码字段。如果没有与之匹配的用户,您将得到零。

将其放入您的模型中

   private
    def self.hash_password(password)
       Digest::SHA1.hexdigest(password)
    end 

事实上,就是这样。。。为什么Rails在大小为1时不拾取集合的第一个对象??(可能用于轨道4;-))