初めに
- Progateで学習して躓いたところを中心にアウトプットすることで学習効率を上げると同時に、備忘録がてらに記事を書いていきます!
いいねの数を数える機能を実装する
posts_controllerのshowメソッドにて@like_countメソッドを定義していいね機能を実装していく
whereとcountメソッド利用していいねの数を数えていく
コード
show.html
<div class="main posts-show"> <div class="container"> <div class="posts-show-item"> <div class="post-user-name"> <img src="<%= "/user_images/#{@user.image_name}" %>"> <%= link_to(@user.name, "/users/#{@user.id}") %> </div> <p> <%= @post.content %> </p> <div class="post-time"> <%= @post.created_at %> </div> <% if Like.find_by(user_id: @current_user.id, post_id: @post.id) %> <%= link_to("/likes/#{@post.id}/destroy", {method: "post"}) do %> <span class="fa fa-heart liked-btn"></span> <% end %> <% else %> <%= link_to("/likes/#{@post.id}/create", {method: "post"}) do %> <span class="fa fa-heart unliked-btn"></span> <% end %> <% end %> <!-- 変数@likes_countを表示してください --> <% if @post.user_id == @current_user.id %> <div class="post-menus"> <%= link_to("編集", "/posts/#{@post.id}/edit") %> <%= link_to("削除", "/posts/#{@post.id}/destroy", {method: "post"}) %> </div> <% end %> </div> </div> </div>
posts_controller
class PostsController < ApplicationController before_action :authenticate_user before_action :ensure_correct_user, {only: [:edit, :update, :destroy]} def index @posts = Post.all.order(created_at: :desc) end def show @post = Post.find_by(id: params[:id]) @user = @post.user end def new @post = Post.new end def create @post = Post.new( content: params[:content], user_id: @current_user.id ) if @post.save flash[:notice] = "投稿を作成しました" redirect_to("/posts/index") else render("posts/new") end end def edit @post = Post.find_by(id: params[:id]) end def update @post = Post.find_by(id: params[:id]) @post.content = params[:content] if @post.save flash[:notice] = "投稿を編集しました" redirect_to("/posts/index") else render("posts/edit") end end def destroy @post = Post.find_by(id: params[:id]) @post.destroy flash[:notice] = "投稿を削除しました" redirect_to("/posts/index") end def ensure_correct_user @post = Post.find_by(id: params[:id]) if @post.user_id != @current_user.id flash[:notice] = "権限がありません" redirect_to("/posts/index") end end end
post_controllerにてshowメソッド内で@likes_countを定義する
@likes_countの内容はlikesテーブルに含まれているpost_idの数を数える
@likes_count = Like.where(post_id : @post.id).count
@postは showメソッドの前半部分の記載より
@post = Post.find_by(id :params[:post.id])
- よってshowメソッドの中身は
@post = Post.find_by(id: params[:id]) @user = @post.user @likes_count = Like.where(post_id: @post.id).count
show.htmlにlikes_countを貼り付ける
<div class="main posts-show"> <div class="container"> <div class="posts-show-item"> <div class="post-user-name"> <img src="<%= "/user_images/#{@user.image_name}" %>"> <%= link_to(@user.name, "/users/#{@user.id}") %> </div> <p> <%= @post.content %> </p> <div class="post-time"> <%= @post.created_at %> </div> <% if Like.find_by(user_id: @current_user.id, post_id: @post.id) %> <%= link_to("/likes/#{@post.id}/destroy", {method: "post"}) do %> <span class="fa fa-heart liked-btn"></span> <% end %> <% else %> <%= link_to("/likes/#{@post.id}/create", {method: "post"}) do %> <span class="fa fa-heart unliked-btn"></span> <% end %> <% end %> <!-- 変数@likes_countを表示してください --> <%= @likes_count %> <% if @post.user_id == @current_user.id %> <div class="post-menus"> <%= link_to("編集", "/posts/#{@post.id}/edit") %> <%= link_to("削除", "/posts/#{@post.id}/destroy", {method: "post"}) %> </div> <% end %> </div> </div> </div>
結語
いいねの数を数える場合はcountメソッドを利用する
いいねの数を数えるメソッドlikes_countの内容はlikesテーブルに存在するpost_idカラムのある特定のidの数を数える
@likes_count = Like.where(post_id: @post.id).count