неопределенная локальная переменная или метод hashed_password для пользовательской модели - PullRequest
1 голос
/ 20 апреля 2011

Я пытаюсь настроить модель User для успешного сохранения пользователя в БД, но мне мешает, NameError: неопределенная локальная переменная или метод hashed_password для #

Модель пользователя:


require 'digest'
class User < ActiveRecord::Base
  attr_accessor :password

  validates :email, :uniqueness => true, 
                    :length => { :within => 5..50 }, 
                    :format => { :with => /^[^@][\w.-]+@[\w.-]+[.][a-z]{2,4}$/i }
  validates :password, :confirmation => true,
                       :length => { :within => 4..20 },
                       :presence => true,
                       :if => :password_required?

  has_one :profile
  has_many :articles, :order => 'published_at DESC, title ASC',
                      :dependent => :nullify
  has_many :replies, :through => :articles, :source => :comments

  before_save :encrypt_new_password

  def self.authenticate(email, password)
    user = find_by_email(email)
    return user if user && user.authenticated?(password)
  end

  def authenticated?(password)
    self.hashed_password == encrypt(password)
  end

  protected
    def encrypt_new_password
      return if password.blank?
      self.hashed_password = encrypt(password)
    end

    def password_required?
      hashed_password.blank? || password.present?
    end

    def encrypt(string)
      Digest::SHA1.hexdigest(string)
    end
end

Ответы [ 3 ]

1 голос
/ 20 апреля 2011

Добавьте поле hashed_password в таблицу users с помощью миграции.Это в настоящее время отсутствует.

0 голосов
/ 30 апреля 2018

Добавить

attr_accessor :password, :password_confirmation 

to Users.rb

0 голосов
/ 20 апреля 2011

Моя первая ставка в том, что hashed_password не определен как столбец в вашей модели. Возможно, вы захотите проверить файл миграции для этой конкретной модели

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...