前提
非同期でフォロー機能を実装済み
実現したいこと
フォロー数とフォロワー数を表示させたい
発生している問題・エラーメッセージ
**「undefined method `following'」**というエラーメッセージが表示されます。
該当のソースコード
【モデル】
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 :post_images, dependent: :destroy has_many :favorites, dependent: :destroy has_many :fav_post_images, through: :favorites, source: :post_image has_many :cosplay_favorites, dependent: :destroy has_many :cosplay_fav_post_images, through: :favorites, source: :post_image has_many :post_comments, dependent: :destroy has_many :relationships has_many :followings, through: :relationships, source: :follow has_many :reverse_of_relationships, class_name: 'Relationship', foreign_key: 'follow_id' has_many :followers, through: :reverse_of_relationships, source: :user validates :name, presence: true, length: { in: 2..15 } attachment :profile_image def follow(other_user) unless self == other_user self.relationships.find_or_create_by(follow_id: other_user.id) end end def unfollow(other_user) relationship = self.relationships.find_by(follow_id: other_user.id) relationship.destroy if relationship end def following?(other_user) self.followings.include?(other_user) end end
relationship.rb
class Relationship < ApplicationRecord belongs_to :user # userモデルを参照する belongs_to :follow, class_name: 'User' validates :user_id, presence: true validates :follow_id, presence: true end
【コントローラー】
users_controller.rb
class UsersController < ApplicationController def show @user = User.find(params[:id]) @post_images = @user.post_images.page(params[:page]).reverse_order end def edit @user = User.find(params[:id]) end def update @user = User.find(params[:id]) @user.update(user_params) redirect_to user_path(@user.id) end def following @user = User.find(params[:id]) @users = @user.following render 'show_follow' end def followers @user = User.find(params[:id]) @users = @user.followers render 'show_follower' end private def user_params params.requrie(:user).permit(:name, :profile_image) end end
relationships_controller.rb
class RelationshipsController < ApplicationController before_action :set_user def create following = current_user.follow(@user) following.save respond_to do |format| format.html { redirect_to @user } format.js end end def destroy following = current_user.unfollow(@user) following.destroy respond_to do |format| format.html { redirect_to @user } format.js end end private def set_user @user = User.find(params[:follow_id]) end end
【ビュー】
users/show.html.erb
<div class="header"> <nav class="navigation"> <img src="/assets/logo.png"> <ul> <li> <%= link_to "ログアウト", destroy_user_session_path, method: :delete %> </li> <li> <%= link_to '投稿一覧', root_path %> </li> <li> <%= link_to 'マイページ', user_path(current_user.id) %> </li> </ul> </nav> </div> <div class="mypage_wrapper"> <div class="profile_image"> <%= attachment_image_tag @user, :profile_image, format: 'jpeg', fallback: "no_image.jpg" %> </div> <div class="profile_content"> <h1><%= @user.name %></h1> <% if @user.id == current_user.id %> <%= link_to "プロフィール編集", edit_user_path(@user), class: "profile_edit_path" %> <% end %> <%= link_to following_user_path(@user) do %> <%= @user.following.count %> <% end %> <%= link_to followers_user_path(@user) do %> <%= @user.followers.count %> <% end %> </div> </div>
users/show_follow.html.erb
<% @user.following.each do |user| %> <%= user.name %> <%= link_to '詳細', user_path(user) %> <% end %>
users/show_follower.html.erb
<% @user.followers.each do |user| %> <%= user.name %> <%= link_to '詳細', user_path(user) %> <% end %>
【ルーティング】
routes.rb
Rails.application.routes.draw do devise_for :users root 'post_images#index' resources :post_images, only: [:new, :create, :index, :show, :destroy] do resource :post_comments, only: [:create, :destroy] end resources :users do member do get :following, :followers end end post '/favorite/:id' => 'favorites#favorite', as: 'favorite' post '/cosplay_favorite/:id' => 'cosplay_favorites#favorite', as: 'cosplay_favorite' resources :relationships, only: [:create, :destroy] # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html end
試したこと
followingがusers_controllerで定義されていないというエラーメッセージだと思うのですが、
def following
@user = User.find(params[:id])
@users = @user.following←ここで定義しているという認識でした。
render 'show_follow'
end
疑問点
・@users = @user.followingのfollowingはどこからきているのか
・users_controllerの@usersを定義している意味
補足情報(FW/ツールのバージョンなど)
ruby 2.5.7p206
Rails 5.2.4.1
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/01/17 05:29