cant use has_secure_password, password_digest error
Asked Answered
H

4

11

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
Hostel answered 13/10, 2011 at 18:54 Comment(1)
Which version of Rails are you using? I may be wrong, but if you use the latest 3.1.1, I think has_secure_password requires the gem bcrypt-ruby ~> 3.0.0.Ancilla
R
27

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.

Roadside answered 20/10, 2011 at 17:12 Comment(0)
S
5

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"
}
Siglos answered 13/3, 2018 at 18:29 Comment(0)
S
3

I had the same problem, I was following http://www.railstutorial.org/book/modeling_users and my app/Controllers/users_controllers.rb didn't have a method to create the attribute, I also used git to share the working code between portable laptop for the train and larger home, this created the migration file but didnt apply it, my working user controller is below.

class UsersController < ApplicationController
    def new
      attr_accessor :name, :email, :password
      def initialize(attributes = {})
      @name  = attributes[:name]
      @email = attributes[:email]
      @password = attributes[:password]
    end

    def formatted_email
      "#{@name} <#{@email}>"
    end
end
Spinks answered 13/6, 2014 at 16:48 Comment(0)
W
1

Hey I'm following RoR too and come into the same problem. The trick here is your bundle exec rake db:migrate fails and therefore the password_digest column hasn't been added into the database. My console complains that database for User already exists. I delete the db/development.sqlite3 manully with "SQLite Browser". After running bundle exec rake db:migrate, every test passes

Wretch answered 23/9, 2012 at 1:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.