Implementation of like function (Ajax)

I will put it together for myself.

Implementation

** Controller model created **

routes.erb


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

likes becomes ** resource ** because it is not necessary to give an ID

post.rb


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

You can check if you like it

likes_controller.rb


class LikesController < ApplicationController

  def create
    @post = Post.find_by(id: params[:post_id])
    #Conditions to prevent continuous clicks
    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

Since redirects are not required for create and destroy respectively, delete them if they are listed.

ruby:posts/show.html.slim


#Give an id because it is necessary to make it Ajax
div id='like_#{ @post.id }'
   #Partial like icon
   = render 'like', post: @post

ruby:posts/_like.html.slim


#Like Delete
- 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

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

Call a JavaScript file by adding ** remote: true **

ruby:likes/create.js.erb


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

ruby:likes/destroy.js.erb


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

**Complete! ** **

Recommended Posts

Implementation of like function (Ajax)
[Rails] Implementation of like function
Implementation of like function in Java
Implementation of pagination function
[Ruby on rails] Implementation of like function
Implementation of image preview function
[Rails] Implementation of category function
Implementation of category pull-down function
[Rails] Implementation of tutorial function
Rails sorting function implementation (displayed in order of number of like)
Implementation of user authentication function using devise (1)
Implementation of user authentication function using devise (3)
[JQuery] Implementation procedure of AutoComplete function [Java / Spring]
Implementation of search function Learning memo (portfolio creation)
Implementation of delete function (if you have foreign_key)
Applied implementation of chat-space
Search function [copype implementation]
[Rails] Implementation of multi-layer category function using ancestry "Preparation"
[Rails] Implementation of multi-layer category function using ancestry "seed"
[Java] Implementation of Faistel Network
About adding a like function
Implementation of tri-tree in Java
Chat function that is updated immediately (implementation of Action Cable)
Implementation of HashMap in kotlin
Let's create a TODO application in Java 8 Implementation of editing function
[Rails] [jQuery] Asynchronous like function implementation using remote: true and js.erb
[Rails] Implementation of coupon function (with automatic deletion function using batch processing)
[Rails] Implementation of tag function using acts-as-taggable-on and tag input completion function using tag-it
Implementation of asynchronous processing in Tomcat
Implementation of validation using regular expressions
[Rails] Implementation of many-to-many category functions
[Rails] gem ancestry category function implementation
[Ruby on Rails] Comment function implementation
Default implementation of Object.equals () and Object.hashCode ()
[Rails] Comment function implementation procedure memo
Error encountered in tagging function implementation
[Rails] I will explain the implementation procedure of the follow function using form_with.
Introduction of Sudoku answer application "Sudoku automatic answer" (Android version) and its implementation function