前提・実現したいこと
Railsで投稿を「いいね」したユーザー一覧を表示させたいのですが、エラーが発生してから先に進めません。
ログイン機能及び複数のユーザー登録、「いいね」機能は既に実装してあります(各投稿にも複数のユーザーが「いいね」を既にしている状態です)。
どなたかお知恵を拝借頂けると幸いです。
発生している問題・エラーメッセージ
NoMethodError in Posts#show Showing C:/hoge/app/views/users/_likes.html.erb where line #1 raised: undefined method `each' for nil:NilClass Extracted source (around line #1): <% @likes.each do |like| %> <% user = User.find_by(id: like.user_id) %> <div class="users-index-item"> <div class="user-left"> <img src="<%= "/user_images/#{user.image_name}" %>"> </div>
該当のソースコード(routes.rb)
ruby
1Rails.application.routes.draw do 2 post "likes/:post_id/create" => "likes#create" 3 post "likes/:post_id/destroy" => "likes#destroy" 4 5 get "login" => "users#login_form" 6 post "login" => "users#login" 7 post "logout" => "users#logout" 8 9 post "users/:id/update" => "users#update" 10 get "users/:id/edit" => "users#edit" 11 post "users/create" => "users#create" 12 get "signup" => "users#new" 13 get 'users/index' => 'users#index' 14 get "users/:id" => "users#show" 15 get "users/:id/likes" => "users#likes" 16 get "/" => "users#top" 17 post "users/:id/destroy" => "users#destroy" 18 19 get 'posts/index'=> 'posts#index' 20 get "posts/new" => "posts#new" 21 get "posts/:id" => "posts#show" 22 post "posts/create" => "posts#create" 23 get "posts/:id/edit" => "posts#edit" 24 post "posts/:id/update" => "posts#update" 25 post "posts/:id/destroy" => "posts#destroy" 26 get "posts/search" => "posts#search" 27 get "posts/:id/likes" => "posts#likes" 28 29 get 'about/about' 30 get 'home/top' => 'home#top' 31 get 'top' => 'home#top' 32 get 'about' => 'about#about' 33 34 resources :users do 35 member do 36 get :following, :followers, :users_tweets 37 end 38 end 39 resources :relationships, only: [:create, :destroy] 40 41 # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html 42end
該当のソースコード(posts_controller.rb)
ruby
1class PostsController < ApplicationController 2 before_action :authenticate_user 3 before_action :ensure_correct_user, {only: [:edit, :update, :destroy]} 4 5 def index 6 @posts = Post.all.order(created_at: :desc) 7 end 8 9 def show 10 @post = Post.find_by(id: params[:id]) 11 @user = @post.user 12 @likes_count = Like.where(post_id: @post.id).count 13 end 14 15 def new 16 @post = Post.new 17 end 18 19 def create 20 @post = Post.new( 21 title: params[:title], 22 level: params[:level], 23 maximum: params[:maximum], 24 message: params[:message], 25 user_id: @current_user.id 26 ) 27 if @post.save 28 flash[:notice] = "投稿を作成しました" 29 redirect_to("/posts/index") 30 else 31 render("posts/new") 32 end 33 end 34 35 def edit 36 @post = Post.find_by(id: params[:id]) 37 end 38 39 def update 40 @post = Post.find_by(id: params[:id]) 41 @post.title = params[:title] 42 @post.level = params[:level] 43 @post.maximum = params[:maximum] 44 @post.message = params[:message] 45 if @post.save 46 flash[:notice] = "投稿を編集しました" 47 redirect_to("/posts/index") 48 else 49 render("posts/edit") 50 end 51 end 52 53 def destroy 54 @post = Post.find_by(id: params[:id]) 55 @post.destroy 56 flash[:notice] = "投稿を削除しました" 57 redirect_to("/posts/index") 58 end 59 60 def ensure_correct_user 61 @post = Post.find_by(id: params[:id]) 62 if @post.user_id != @current_user.id 63 flash[:notice] = "権限がありません" 64 redirect_to("/posts/index") 65 end 66 end 67 68 def search 69 #Viewのformで取得したパラメータをモデルに渡す 70 @posts = Post.search(params[:search]) 71 end 72 73 def likes 74 @post = Post.find_by(id: params[:id]) 75 @likes = Like.where(post_id: @post.id) 76 end 77end
該当のソースコード(post#show.html.erb)
ruby
1<div class="main posts-show"> 2 <div class="container"> 3 <div class="posts-show-item"> 4 <div class="post-user-name"> 5 <!-- <img src=" <%= "/user_images/#{@user.image_name}" %> "> --> 6 <!-- <%=link_to(@user.name, "/users/#{@user.id}") %> --> 7 </div> 8 <ul> 9 <li> 10 <label for="user">Organizer:</label> 11 <%=link_to(@user.name, "/users/#{@user.id}", class: "link") %> 12 </li> 13 14 <li> 15 <label for="level">Level:</label> 16 <%= @post.level %> 17 </li> 18 19 <li> 20 <label for="title">Title:</label> 21 <%= @post.title %> 22 </li> 23 24 <li> 25 <label for="maximum">Maximum participants:</label> 26 <%= @post.maximum %> 27 </li> 28 29 <li> 30 <label for="maximum">Schedule:</label> 31 <%= @post.maximum %> 32 Schedule: 33 </li> 34 35 <li> 36 Rules: 37 </li> 38 <li> 39 <label for="message">Message:</label> 40 <%= @post.message %> 41 </li> 42 </ul> 43 <div class="post-time"> 44 <%= @post.created_at %> 45 </div> 46 <% if Like.find_by(user_id: @current_user.id, post_id: @post.id) %> 47 <%= link_to("/likes/#{@post.id}/destroy", {method: "post"}) do %> 48 <span class="fa fa-heart like-btn-unlike"></span> 49 <% end %> 50 <% else %> 51 <%= link_to("/likes/#{@post.id}/create", {method: "post"}) do %> 52 <span class="fa fa-heart like-btn"></span> 53 <% end %> 54 <% end %> 55 <%= @likes_count %> 56 <h3> 57 <%= render 'users/likes' %> 58 </h3> 59 60 <% if @post.user_id == @current_user.id %> 61 <div class="post-menus"> 62 <ul> 63 <li> 64 <%= link_to("Edit", "/posts/#{@post.id}/edit", class: "link") %> 65 </li> 66 67 <li> 68 <%= link_to("Delete", "/posts/#{@post.id}/destroy", {method: :post, data: {confirm: "Are you sure to delete this event?"}, class: "link"}) %> 69 </li> 70 </ul> 71 </div> 72 <% end %> 73 </div> 74 </div> 75</div>
該当のソースコード(posts#_likes.html.erb)
ruby
1<% @likes.each do |like| %> 2 <% user = User.find_by(id: like.user_id) %> 3 <div class="users-index-item"> 4 <div class="user-left"> 5 <img src="<%= "/user_images/#{user.image_name}" %>"> 6 </div> 7 <div class="user-right"> 8 <%= link_to(user.name, "/users/#{user.id}") %> 9 </div> 10 </div> 11<% end %>
試したこと
自分(ログイン中のユーザー)が「いいね」した投稿一覧は表示できたのでその逆の要領でコードを書いてみましたが、どこかがおかしい様です。
補足情報(FW/ツールのバージョンなど)
ruby 2.6.4p104
RubyGems 3.0.3
Rails 5.2.3
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/02/11 12:10