下記のエラーが出ています。検索したり、relationshipsのコントローラーで初期化の定義してみましたが解決しません。初学者ですので解説もいただけると助かります。
NameError in Users#show
Showing /home/ec2-user/environment/rails6-bookers2-debug-ruby3-main/app/views/users/_info.html.erb where line #13 raised:
uninitialized constant User::Relationships
raise NameError.new("uninitialized constant #{candidates.first}", candidates.first)
^^^^^
Extracted source (around line #13):
11
12
13
14
15
16
該当すると思われるコードを記載します。見づらい部分もあると思いますが、よろしくお願いします。
_info html.erb
table class='table'>
<tr><%= image_tag user.get_profile_image(100,100) %></tr>
<tr>
<th>name</th>
<th><%= user.name %></th>
</tr>
<tr>
<th>introduction</th>
<th><%= user.introduction %></th>
</tr>
<tr>
<th>follows</th>
<th><%= link_to user.followings.count, user_followings_path(user) %></th>
</tr>
<tr>
<th>followers</th>
<th><%= link_to user.followers.count, user_followers_path(user) %></th>
</tr>
</table>
<div class='row'>
<%= link_to edit_user_path(user), class: "btn btn-outline-secondary btn-block" do %>
<i class="fas fa-user-cog"></i>
<% end %>
</div>
class RelationshipsController < ApplicationController
before_action :authenticate_user!
def create
@user = User.find(params[:user_id])
current_user.follow(params[:user_id])
redirect_to request.referer
end
def destroy
current_user.unfollow(params[:user_id])
redirect_to request.referer
end
# フォロー一覧
def followings
user = User.find(params[:user_id])
@users = user.followings
end
# フォロワー一覧
def followers
user = User.find(params[:user_id])
@users = user.followers
end
def count(user)
@count_following = user.following.count
@count_follower = user.follower.count
end
end
relationships.rb
class Relationship < ApplicationRecord
belongs_to :follower, class_name: "User"
belongs_to :followed, class_name: "User"
end
user.rb
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
has_many :books
has_one_attached :profile_image
has_many :favorites, dependent: :destroy
has_many :book_comments, dependent: :destroy
has_many :relationships, class_name: "Relationships", foreign_key: "follower_id", dependent: :destroy
has_many :followings, through: :relationships, source: :follow
has_many :reverse_relationships, class_name: "Relationships", foreign_key: "followed_id", dependent: :destroy
has_many :followers, through: :reverse_relationships, source: :follower
validates :name, presence:true, length: { minimum: 2, maximum: 20 }, uniqueness: true
validates :introduction, length: { maximum: 50 }
def get_profile_image(a,b)
unless profile_image.attached?
file_path = Rails.root.join('app/assets/images/no_image.jpg')
profile_image.attach(io: File.open(file_path), filename: 'no_image.jpg', content_type: 'image/jpeg')
end
profile_image.variant(resize_to_limit: [a, b]).processed
end
def follow(follower_ids)
relationships.create(followed_id: user_id)
end
def unfollow(follower_ids)
relationships.find_by(followed_id: user_id).destroy
end
def following?(user)
followings.include?(user)
end
end
<div class='container px-5 px-sm-0'>
<div class='row'>
<div class='col-md-3'>
<h2>User info</h2>
<%= render 'info', user: @user %>
<h2 class="mt-3">New book</h2>
<%= render 'books/form', book: @book %>
</div>
<div class='col-md-8 offset-md-1'>
<h2>Books</h2>
<%= render 'books/index',books: @books %>
</div>
</div>
</div>