Rails教程第11章错误“未初始化的常量User :: Relationships”

时间:2013-02-07 04:19:25

标签: ruby-on-rails syntax-error railstutorial.org

我一直收到错误

   uninitialized constant User::Relationships

在完成rails教程的第11章时。

当我在浏览器中登录时尝试访问主页时出现完整错误。

    Extracted source (around line #11):

8:          </a>
9:          <a href="<%= followers_user_path(@user) %>">
10:         <strong id="followers" class="stat">
11:             <%= @user.followers.count %>
12:         </strong>
13:         followers
14:     </a>

我已多次浏览本章并检查每一行代码,但有时你的眼睛会欺骗你,所以这里是代码的其余部分

users.rb的

class User < ActiveRecord::Base
attr_accessible :email, :name, :password, :password_confirmation
has_secure_password

has_many :microposts, dependent: :destroy 
has_many :relationships, foreign_key: "follower_id", dependent: :destroy
has_many :followed_users, through: :relationships, source: :followed 
has_many :reverse_relationships, foreign_key: "followed_id",
                               class_name: "Relationships",
                               dependent:   :destroy
has_many :followers, through: :reverse_relationships, source: :follower

before_save { |user| user.email = email.downcase}
before_save :create_remember_token

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

VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, format: { with: VALID_EMAIL_REGEX},
                uniqueness: { case_sensitive: false }
validates :password, presence: true, length: { minimum: 6}
validates :password_confirmation, presence: true

def feed
  Micropost.where("user_id =?", id)
end

def following?(other_user)
  relationships.find_by_followed_id(other_user.id)
end

def follow!(other_user)
  relationships.create!(followed_id: other_user.id)
end

def unfollow!(other_user)
  relationships.find_by_followed_id(other_user.id).destroy
end

private
  def create_remember_token
    self.remember_token = SecureRandom.urlsafe_base64
  end

end

这是班级本身

class Relationship < ActiveRecord::Base
  attr_accessible :followed_id
  belongs_to :follower, class_name: "User"
  belongs_to :followed, class_name: "User"

  validates :follower_id, presence: true
  validates :followed_id, presence: true
end

这就是教程中的内容......我添加了:follower_id,以防万一,但仍然无效。

我还建立了一个relationship_controller。

class RelationshipsController < ApplicationController
before_filter :signed_in_user

def create
  @user = User.find(params[:relationship][:follower_id])
  current_user.follow!(@user)
  respond_to do |format|
    format.html { redirect_to @user }
    format.js
  end
end

def destroy
  @user = Relationship.find(params[:id]).followed
  current_user.unfollow!(@user)
  respond_to do |format|
    format.html { redirect_to @user }
    fromat.js
  end
end

在路线......

 resources :users do
   member do
     get :following, :followers
   end
 end

发生错误的页面如下所示:

<% @user ||= current_user %>
<div class = "stats">
<a href ="<%= following_user_path(@user)%>">
    <strong id="following" class="stat">
        <%= @user.followed_users.count %>
    </strong>
    following
</a>
<a href="<%= followers_user_path(@user) %>">
    <strong id="followers" class="stat">
        <%= @user.followers.count %>
    </strong>
    followers
</a>
</div>

第二个块之前的代码的第一部分在我删除第二部分时完美地工作。只是“追随者”的关系由于某种原因没有得到建立。我在控制台中玩它并没有调用,而user.followed_users确实有效。我一直在玩这个四个小时,放下桌子并重建它,我无法让它工作。

我之前尝试过查看堆栈溢出并发现:

Ruby error (uninitialized constant User::Relationship)

但这些解决方案都没有帮助。谢谢你的帮助!

1 个答案:

答案 0 :(得分:5)

你有一个错字:

has_many :reverse_relationships, foreign_key: "followed_id",
                                 class_name: "Relationships",

将最后一位更改为class_name: "Relationship"