ruby-on-rails - Ruby 错误(未初始化的常量 User::Relationship)

标签 ruby-on-rails ruby initialization

我是 ruby​​ 的新手,正在尝试学习 Michael Hartl 的 http://ruby.railstutorial.org/ .我在第 12 章,不断遇到这个错误

未初始化的常量 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

最佳答案

您还没有定义 relationship_controller.rb 代码。继续阅读本章,他确实解释了最终需要完成的工作。

关于ruby-on-rails - Ruby 错误(未初始化的常量 User::Relationship),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5304421/

相关文章:

swift - 如何在 Swift 中为 UIViewController 子类创建自定义初始值设定项?

ruby-on-rails - Rails 的 ActiveRecord::Migration 的外键?

ruby-on-rails - rails : undefined method error when move codes from view to helper file

ruby-on-rails - 覆盖 Spree Commerce 的 Bootstrap 变量

ruby - 在 Windows 上安装 "kgio-2.9.2"Gem 时出错

ruby - Ruby 数学运算的精度问题

java - 明确分配变量

events - FLutter - 如何在 BLoC 初始化时分派(dispatch)事件

ruby-on-rails - 应该将 Ruby on Rails 项目的哪些部分检入存储库?

ruby-on-rails - 如何寻址 "DEPRECATION WARNING: ActiveSupport::Memoizable is deprecated and will be removed in future releases"?