1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

いいね機能(Ajax)実装

Last updated at Posted at 2020-07-21

自分用にまとめます。

実装

コントローラー・モデル作成済み

routes.erb
  Rails.application.routes.draw do
    resources :posts do
      resources :comments, only: [:create, :destroy]
      resource :likes, only: [:create, :destroy]
    end
  end

likesはIDの付与不必要のためresourceになる


post.rb
  def liked_by?(user)
    likes.where(user_id: user.id).exists?
  end

いいねしたかどうかを確認できる


likes_controller.rb
class LikesController < ApplicationController

  def create
    @post = Post.find_by(id: params[:post_id])
    # 連続クリック防止のための条件
    unless @post.liked_by?(current_user)
      @like = current_user.likes.new(post_id: @post.id)
      @like.save
    end
  end

  def destroy
    @post = Post.find_by(id: params[:post_id])
    @like = current_user.likes.find_by(post_id: @post.id)
    @like.destroy
  end

end

create,destroyそれぞれリダイレクトは不要になるので、記載している場合は削除


posts/show.html.slim
#Ajax化するために必要になるのでidを付与する
div id='like_#{ @post.id }'
   # いいねアイコンをパーシャル化
   = render 'like', post: @post
posts/_like.html.slim
# いいね削除
- if post.liked_by?(current_user)
  = link_to post_likes_path(post), method: :delete, remote: true do
    i.fas.fa-heart style='color: red;'
    = post.likes.count

# いいね
- else
  = link_to post_likes_path(post), method: :post, remote: true do
    i.far.fa-heart
    = post.likes.count

remote: trueをつけることでJavaScriptファイルを呼び出す


likes/create.js.erb
$("#like_<%= @post.id %>").html("<%= j(render 'posts/like', post: @post ) %>");
likes/destroy.js.erb
$("#like_<%= @post.id %>").html("<%= j(render 'posts/like', post: @post ) %>");

完成!

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?