未初始化的常量 User::follow 提取的源代码围绕第 28 行:

问题描述

我的 user.rb 模型中有错误(大约第 28 行):

27def following?(user) 
28 following.include?(user)
29end

知道我在这里做错了什么吗? 显示 C:/instagramm/instagram-clone/app/views/accounts/profile.html.erb 其中第 11 行提出: 我的 profile.html.erb :

<% if @users.image.present %>
<%= image_tag @users.image %>
<% end %>


<strong><h1><%= @users.full_name %></h1></strong>

<% if user_signed_in? && @user == current_user %>

      <%= link_to"Edit Profile",edit_user_registration_path(@user) %>
    <% if current_user.following?(@user) %>
        <%= link_to"Unfollow",follows_path(user_id: @user.id),method: :delete %>
    <% else %>
        <%= link_to"Follow",follows_path(user_id: @user.id) %>
    <% end %>
<% end %>


<div> <%= @users.posts.count %> Posts </div>




<p><%= @users.full_name %></p>
<p><%= @users.description %></p>
<p><%= link_to 'User Website',@users.website if @users.website.present? %></p>



<%= @posts.each do |post|%>
<%= image_tag post.image %>
<% end %>

我的模型:follow.rb

class Follow < ApplicationRecord
    
    

    belongs_to :follower,class_name: 'user'
    belongs_to :followed,class_name: 'user'

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

这是我的 user.rb 模型:错误在 (#line28) in def following?(user)

class User < ApplicationRecord

  has_many :posts 
  validates :username,presence: true 
  # Include default devise modules. Others available are:
  # :confirmable,:lockable,:timeoutable,:trackable and :omniauthable
  devise :database_authenticatable,:registerable,:recoverable,:rememberable,:validatable

has_one_attached :image

has_many :active_follows,class_name: "follow",foreign_key: "follower_id",dependent: :destroy 

has_many :passive_follows,foreign_key: "followed_id",dependent: :destroy

has_many :following,through: :active_follows,source: :followed
has_many :followers,through: :passive_follows,source: :follower

def follow(user)
  active_follows.create(followed_id: user.id)
end

def unfollow(user)
  active_follows.find_by(followed_id: user.id).destroy 
end

def following?(user)
following.include?(user)
end 

def full_name
  "#{first_name} #{last_name}"
end



end

解决方法

Rails 假定关联指向具有匹配名称的类,因此在这种情况下,您的 following 关联将搜索 Following 类。

显然这不是您在这里需要的 - 通过代码结构快速猜测您希望 following 返回用户集合,因此您需要将其告知您的关联:

has_many :following,through: :active_follows,source: :followed,class_name: 'User'

使用正确的类名给 class_name 选项也很重要。它必须与 ActiveRecord 类的名称完全匹配,因此它必须是 "Follow" 而不是 "follow",类似地,"User" 不是 "user"

,

您已经收到@BroiStase 的回复,您在代码中使用了错误的类名称。请更改您的 Follow.rb

class Follow < ApplicationRecord
    belongs_to :follower,class_name: 'User'
    belongs_to :followed,class_name: 'User'
    validates :follower_id,presence: true
    validates :followed_id,presence: true
end

在您的 User.rb 之后,您必须进行以下更改。

class User < ApplicationRecord
  has_many :posts 
  validates :username,presence: true 
  # Include default devise modules. Others available are:
  # :confirmable,:lockable,:timeoutable,:trackable and :omniauthable
  devise :database_authenticatable,:registerable,:recoverable,:rememberable,:validatable
  has_one_attached :image
  has_many :active_follows,class_name: "Follow",foreign_key: "follower_id",dependent: :destroy 
  has_many :passive_follows,foreign_key: "followed_id",dependent: :destroy
  has_many :following,source: :followed
  has_many :followers,through: :passive_follows,source: :follower

  def follow(user)
    active_follows.create(followed_id: user.id)
  end

  def unfollow(user)
    active_follows.find_by(followed_id: user.id).destroy 
  end

  def following?(user)
    following.include?(user)
  end 

  def full_name
    "#{first_name} #{last_name}"
  end
end

请在编写代码时避免任何额外的空格、换行符和适当的 2 个空格缩进,这将有助于您更好地理解代码。如果仍然错误,请分享错误信息。