登录应用程序时出现错误“未知属性:密码”。它似乎没有保存密码。我是rails新手,有人可以帮我吗?
这是我在运行应用程序时收到的消息。这是模型和控制文件中的代码。用户控制器中的 ActiveRecord::UnknownAttributeError#create
未知属性:密码
我正在尝试创建一个登录名/密码网络应用程序。运行应用程序时出现以下错误。未定义的方法encrypt_pasword'
app/models/user.rb:32:in
密码='app/controllers/users_controller.rb:43:in new'
app/controllers/users_controller.rb:43:in
create'
这是控制文件中的代码。
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