Rubyuser
Rubyuser

Reputation: 79

unknown attribute: password

I got the error " unknown attribute: password" while logging into the app. It doesn't seem to save the password. I am new to rails, can anyone help me on this?

This is the message that I get while running the app. Here are is the code from model and control files. ActiveRecord::UnknownAttributeError in UsersController#create

unknown attribute: password

I am trying to create a login/password web app. Getting the following error while running the app. undefined method encrypt_pasword' app/models/user.rb:32:inpassword=' app/controllers/users_controller.rb:43:in new' app/controllers/users_controller.rb:43:increate'

Here is the code from control file.

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

Here is my model file

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

Upvotes: 1

Views: 2388

Answers (1)

bento
bento

Reputation: 2099

encrypt_pasword is missing an s.

Upvotes: 2

Related Questions