【问题标题】:Ruby error (uninitialized constant User::Relationship)Ruby 错误(未初始化的常量 User::Relationship)
【发布时间】:2011-03-14 20:56:14
【问题描述】:

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

uninitialized constant User::Relationship

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

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

【问题讨论】:

  • 出现错误是因为您在 Rails 尚未加载它的地方使用 User::Relationship,但如果没有代码的一些细节,将很难提供太多帮助。尝试发布您尝试使用 User::Relationship 类/模块的一些区域。

标签: ruby-on-rails ruby initialization


【解决方案1】:

您还没有定义 relationship_controller.rb 代码。继续阅读这一章,他确实解释了最终需要做什么。

【讨论】:

    【解决方案2】:

    我自己遇到了这个。我没有添加您可以在 11.34 找到的关系控制器 (app/controllers/relationships_controller.rb)。添加后,错误将消失。

    【讨论】:

      【解决方案3】:

      听起来你可能没有按照清单 12.11 中的说明更新 User 模型。

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-02-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多