未初始化的常量 User::follow 提取的源代码(大约第 28 行):
uninitialized constant User::follow Extracted source (around line #28):
我的 user.rb 模型有一个错误(大约第 28 行):
27def following?(user)
28 following.include?(user)
29end
知道我在这里做错了什么吗?
显示 C:/instagramm/instagram-clone/app/views/accounts/profile.html.erb 第 11 行出现的位置:
我的个人资料。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 模型:错误在 def 中的(#line28)附近?(用户)
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, class_name: "follow", 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 假定关联指向具有匹配名称的 class,因此在这种情况下,您的 following
关联将搜索 Following
class。
显然这不是您在这里需要的 - 通过您期望的代码结构快速猜测 following
到 return 一组用户,因此您需要将其告知您的协会:
has_many :following, through: :active_follows, source: :followed, class_name: 'User'
使用给 class_name
选项的正确 class 名称也很重要。它必须与 ActiveRecord 的名称 class 完全匹配,因此它必须是 "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, class_name: "Follow", 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
请在编写代码时避免任何额外的 spaces、换行符并适当缩进 2 space 以帮助您更好地理解代码。如果仍然出错,请也分享错误消息。
我的 user.rb 模型有一个错误(大约第 28 行):
27def following?(user)
28 following.include?(user)
29end
知道我在这里做错了什么吗? 显示 C:/instagramm/instagram-clone/app/views/accounts/profile.html.erb 第 11 行出现的位置: 我的个人资料。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 模型:错误在 def 中的(#line28)附近?(用户)
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, class_name: "follow", 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 假定关联指向具有匹配名称的 class,因此在这种情况下,您的 following
关联将搜索 Following
class。
显然这不是您在这里需要的 - 通过您期望的代码结构快速猜测 following
到 return 一组用户,因此您需要将其告知您的协会:
has_many :following, through: :active_follows, source: :followed, class_name: 'User'
使用给 class_name
选项的正确 class 名称也很重要。它必须与 ActiveRecord 的名称 class 完全匹配,因此它必须是 "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, class_name: "Follow", 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
请在编写代码时避免任何额外的 spaces、换行符并适当缩进 2 space 以帮助您更好地理解代码。如果仍然出错,请也分享错误消息。