我在登录应用时收到错误“unknown attribute:password”。它似乎没有保存密码。我是铁杆新手,有人可以帮我吗?
这是我在运行应用时收到的消息。以下是模型和控制文件的代码。 UsersController中的ActiveRecord :: UnknownAttributeError #create
我正在尝试创建一个登录/密码Web应用程序。运行应用程序时出现以下错误。
未定义的方法encrypt_pasword'
app/models/user.rb:32:in
password ='
app / controllers / users_controller.rb:43:new'
app/controllers/users_controller.rb:43:in
创建'
以下是控制文件中的代码。
require 'digest/sha2'
class User < ActiveRecord::Base
include ActiveModel::MassAssignmentSecurity
attr_accessible :name, :password, :password_confirmation, :hashed_password, :salt
attr_accessor :name
attr_accessor :salt
validates :name, :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)
if user = find_by_name(name)
if user.hashed_password == encrypt_password(password, user.salt)
user
end
end
end
def User.encrypt_password(password, salt)
Digest::SHA2.hexdigest(password + "wibble" + salt)
end
# 'password' is a virtual attribute
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, "Missing password") unless hashed_password.present?
end
def generate_salt
self.salt = self.object_id.to_s + rand.to_s
end
end
这是我的模型文件
require 'digest/sha2'
class User < ActiveRecord::Base
include ActiveModel::MassAssignmentSecurity
attr_accessible :name, :password, :password_confirmation, :hashed_password, :salt
attr_accessor :name
attr_accessor :salt
attr_accessor :password
validates :name, :presence => true, :uniqueness => true
validates :password, :confirmation => true
attr_accessor :password_confirmation
attr_reader :password
validate :password_must_be_present
def encrypt_password
self.encrypted_password
end
def User.authenticate(name, password)
if user = find_by_name(name)
if user.hashed_password == encrypt_password(password, user.salt)
user
end
end
end
def User.encrypt_password(password, salt)
Digest::SHA2.hexdigest(password + "wibble" + salt)
end
private
def encrypt_password
self.salt = make_salt if new_record?
self.encrypted_password = encrypt(password)
end
# 'password' is a virtual attribute
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, "Missing password") unless hashed_password.present?
end
def generate_salt
self.salt = self.object_id.to_s + rand.to_s
end
end
答案 0 :(得分:2)
encrypt_pasword缺少s。