gpt4 book ai didi

ruby-on-rails - Rails教程第11章错误 "uninitialized constant User::Relationships"

转载 作者:行者123 更新时间:2023-12-04 06:15:51 25 4
gpt4 key购买 nike

我一直收到错误

   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>

我已经多次阅读本章并检查了每一行代码,但有时您的眼睛会欺骗您,所以这里是其余代码

用户.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

结束

在 route ...

 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>

当我删除第二部分时,第二个 block 之前的代码的第一部分工作得很好。只是由于某种原因没有建立“追随者”关系。我在控制台中玩弄它,它没有调用,而 user.followed_users 确实有效。我已经玩了四个小时了,把 table 扔了再重建,但我无法让它工作。

我之前尝试查看堆栈溢出并发现了这个:

Ruby error (uninitialized constant User::Relationship)

但是那里的解决方案都没有帮助。感谢您的帮助!

最佳答案

你打错了:

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

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

关于ruby-on-rails - Rails教程第11章错误 "uninitialized constant User::Relationships",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14743478/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com