未定义方法“hashed_password”使用 Rails 进行敏捷 Web 开发
我正在遵循敏捷 Web 开发的迭代 13,用于用户登录。
我创建了一个迁移,将 2 列添加到我的用户模型中:hashed_password 和 salt。
- 创建用户工作
- 由于此错误,
登录失败,方法“authenticate”中未定义方法“hashed_password”问题是:
- 在rails控制台中,我可以获取User.first.hashed_password,看起来不错:-)
- 我输出了用户我 fecth,它不是 nil
- 我尝试输出 user.hashed_password 就像我在 Rails 控制台中所做的那样,但这总是抛出相同的错误:
NoMethodError(未定义) #hashed_password'): app/models/user.rb:21:in
验证' app/controllers/sessions_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
I'm following Iteration 13 of Agile Web development, for User login.
I create a migration to add 2 columns to my User model : hashed_password and salt.
- Creation of Users works
- login fails, due to this error undefined method 'hashed_password' in method 'authenticate'
The problem is that :
- In rails console, I can fetch User.first.hashed_password, what seems OK :-)
- I outputted the User that I fecth, and it is NOT nil
- I tried to output user.hashed_password as I did in the rails console, but that throws always the same error :
NoMethodError (undefined method hashed_password' for #<ActiveRecord::Relation:0x00000003e6c3c0>):
authenticate'
app/models/user.rb:21:in
app/controllers/sessions_controller.rb:6:in `create'
This is my User model :
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
User.where(:username => name) 返回一个 ActiveRecord::Relation 对象(因此您看到的错误消息)。尝试将 if 语句更改为:
这将设置第一个匹配的用户,该用户将是 User 的实例,因此将具有 hashed_password 字段。如果没有用户匹配,您将得到零。
User.where(:username => name) is returning an ActiveRecord::Relation object (hence the error message you are seeing). Try changing your if statement to:
That will set take the first matching user, which will be an instance of User and so will have a hashed_password field. If no user matches, you'll get nil.
把这个放入你的模型中
put this in your model