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
[Rails] Asynchronous implementation of like function
[Rails] About implementation of like function
Implementation of like function in Java
Follow function (Ajax) implementation
Implementation of search function
Implementation of pagination function
[Ruby on rails] Implementation of like function
Rails implementation of ajax removal
[Rails 6] Implementation of search function
Implementation of image preview function
[Rails] Implementation of category function
Implementation of category pull-down function
[Rails] Implementation of tutorial function
[Rails] Implementation of CSV import function
[Rails] Implementation of image preview function
[Rails] Implementation of user withdrawal function
[Rails] Implementation of CSV export function
[Rails 6] Like function (synchronous → asynchronous) implementation
Rails sorting function implementation (displayed in order of number of like)
I tried to implement Ajax processing of like function in Rails
Implementation of user authentication function using devise (2)
Implementation of user authentication function using devise (1)
Implementation of GKAccessPoint
Rails [For beginners] Implementation of comment function
[Rails 6] Implementation of SNS (Twitter) sharing function
Implementation of user authentication function using devise (3)
[Vue.js] Implementation of menu function Implementation version rails6
[Vue.js] Implementation of menu function Vue.js introduction rails6
[Rails] Implementation of search function using gem's ransack
Implementation of Ruby on Rails login function (Session)
[Rails 6] Implementation of inquiry function using Action Mailer
[Rails] Implementation of image enlargement function using lightbox2
[Rails] Implementation of retweet function in SNS application
[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)
Implementation of flash messages
Image preview function implementation
Applied implementation of chat-space
Rails search function implementation
Search function [copype implementation]
Ruby on Rails <2021> Implementation of simple login function (form_with)
[Ruby on Rails] Asynchronous communication of posting function, ajax
[Rails] Implementation of drag and drop function (with effect)
Implementation of Ruby on Rails login function (devise edition)
[Ruby on Rails] Implementation of tagging function/tag filtering function
[Rails] Implementation of multi-layer category function using ancestry "Preparation"
[Rails] Implementation of multi-layer category function using ancestry "seed"
[Rails] Implementation of SNS authentication (Twitter, Facebook, Google) function
Addition of guest login function
About merge processing implementation including sorting function of Stream API
[Rails] Implementation of multi-layer category function using ancestry "Editing form"
[Swift] Simple implementation of UIImageView
Rails fuzzy search function implementation
[Rails] Implementation of multi-layer category function using ancestry "Creation form"
[Swift] Implementation of ultra-simple billing
[Java] Implementation of Faistel Network
Implementation of XLPagerTabStrip with TabBarController
[Rails] Implementation of tagging function using intermediate table (without Gem)