Ruby错误(未初始化的常量User :: Relationship) [英] Ruby error (uninitialized constant User::Relationship)

查看:82
本文介绍了Ruby错误(未初始化的常量User :: Relationship)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是红宝石的新手,正在尝试通过Michael Hartl的 http://ruby.railstutorial.org/.我在第12章,不断遇到这个错误

I am new to ruby and trying to work through Michael Hartl's http://ruby.railstutorial.org/. I am on chapter 12 and keeping coming upon this error

uninitialized constant User::Relationship

这种类型的错误是什么意思?您认为我的错误可能是什么?

What does this type of error mean? What do you think is likely my mistake?

attr_accessor   :password
  attr_accessible :name, :email, :password, :password_confirmation, :admin

  has_many :microposts, :dependent => :destroy
  has_many :relationships, :foreign_key => "follower_id",
                           :dependent => :destroy
  has_many :following, :through => :relationships, :source => :followed

  has_many :reverse_relationships, :foreign_key => "followed_id",
                                  :class_name => "Relationship",
                                  :dependent => :destroy 
  has_many :followers, :through => :reverse_relationships, :source => :follower

  email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i

  validates :name, :presence => true,
                   :length => { :maximum => 50 }

  validates :email, :presence  => true,
                    :format    => { :with => email_regex },
                    :uniqueness => { :case_sensitive => false }

  validates :password, :presence  => true,
                       :confirmation => true,
                       :length => { :within => 6..40 }

  before_save :encrypt_password 

  def has_password?(submitted_password)
    encrypted_password == encrypt(submitted_password)
  end

  def feed
   # This is preliminary. See Chapter 12 for the full implementation.
     Micropost.where("user_id = ?", id) 
  end

  def following?(followed)
    relationships.find_by_followed_id(followed) 
  end

  def follow!(followed) 
    relationships.create!(:followed_id => followed.id)
  end

  def 
    unfollow!(followed) relationships.find_by_followed_id(followed).destroy
  end

  class << self
    def authenticate(email, submitted_password)
      user=self.find_by_email(email)
      (user && user.has_password?(submitted_password)) ? user : nil     
    end   

    def authenticate_with_salt(id, cookie_salt)
      user = find_by_id(id)
      (user && user.salt == cookie_salt) ? user : nil 
    end 
  end

  private 

  def encrypt_password
    self.salt = make_salt if self.new_record?
    self.encrypted_password = encrypt(self.password)
  end

  def encrypt(string)
    string 
  end

  def encrypt(string)
    secure_hash("#{salt}--#{string}")
  end

  def make_salt
    secure_hash("#{Time.now.utc}--#{password}")
  end

  def secure_hash(string) 
    Digest::SHA2.hexdigest(string)
  end  
end

推荐答案

听起来好像您没有按照清单12.11中的说明更新User模型.

It sounds like maybe you didn't update the User model as specified in Listing 12.11.

具体来说,请确保您的User类具有以下代码:

Specifically, make sure your User class has this code:

has_many :relationships, :foreign_key => "follower_id",
                       :dependent => :destroy

这篇关于Ruby错误(未初始化的常量User :: Relationship)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆