Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cant use has_secure_password, password_digest error

Good evening. I have a problem. i am using has_secure_password and cause of this i have an error undefined methodpassword_digest=' for #`,

but i dont have this method!! Please help, dont know what to do. I read how to fix this problem but it didnt help me(

Here is my User model. Please help if you can.

class User < ActiveRecord::Base

  attr_accessible :email, :password, :password_confirmation
  has_secure_password

  validates_presence_of :password, :on => :create

  before_create { generate_token(:auth_token) }

  def send_password_reset
    generate_token(:password_reset_token)
    self.password_reset_sent_at = Time.zone.now
    save!
    UserMailer.password_reset(self).deliver
  end

  def generate_token(column)
    begin
      self[column] = SecureRandom.urlsafe_base64
    end while User.exists?(column => self[column])
  end
end
like image 794
Pavel Avatar asked Oct 13 '11 18:10

Pavel


2 Answers

Models having has_secure_password store password in password_digest column instead of password column. In fact password column is not needed.

> u=User.create!(email: '[email protected]', password: '12345678')
> u
#<User:0x007fc794be9278> {
                  :id => 1,
:email => "[email protected]",
     :password_digest => "$2a$10$S82GVFR..yO9jihgIoeMj.7dNMWtbCUZpWDKvH0tyMs1SYlfdefmW"
}
like image 140
Imran Ahmad Avatar answered Oct 08 '22 09:10

Imran Ahmad


You may have forgotten to make sure your migration backing the user model has a column for password_digest. Make sure the column exists and that it's a string. If it doesn't, create a migration to add the column.

like image 37
ChrisAndy Avatar answered Oct 08 '22 08:10

ChrisAndy