我正在尝试设置用户模型以成功将用户保存在数据库中,但我受到阻碍,NameError:未定义的局部变量或方法`hashed_password'用于#< User:0x000001029fef18>
用户模型:
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
答案 0 :(得分:1)
使用迁移将hashed_password
字段添加到users
表。它目前缺失。
答案 1 :(得分:0)
我的第一个赌注是,hashed_password未定义为模型中的列。可能要检查此特定模型的迁移文件
答案 2 :(得分:0)
添加
public class BinaryTreeNode<T> {
private BinaryTreeNode<T> left;
private BinaryTreeNode<T> right;
private T data;
public BinaryTreeNode(){
this(null,null,null);
}
public BinaryTreeNode(T theData){
this(theData,null,null);
}
public BinaryTreeNode(T theData, BinaryTreeNode<T> leftChild, BinaryTreeNode<T> rightChild){
data = theData;
left = leftChild;
right = rightChild;
@Override
public boolean equals(Object o){
if(o instanceof BinaryTreeNode<?>) {
BinaryTreeNode<?> n1 = (BinaryTreeNode<?>) o;
if(this.getLeft() == null && n1.getLeft() == null && this.getRight() == null && n1.getLeft() == null) {
return this.data == n1.getData();
}
if(this.getRight() == null && n1.getRight() == null && this.getLeft() != null && n1.getLeft() != null) {
return this.data == n1.getData();
}
if(this.getLeft() == null && n1.getLeft() == null && this.getRight() != null && n1.getRight() != null) {
return this.data == n1.getData();
} else {
return false;
}
}
}
public class BinaryTreeTesting {
BinaryTreeNode<Integer> node15 = new BinaryTreeNode<Integer>(5);
BinaryTreeNode<Integer> node2 = new BinaryTreeNode<Integer>(5);
@Test
public void testEqualsObjectNode() {
assertTrue(node15.equals(node2));
assertFalse(node1.equals(node2));
}
到Users.rb