[Ruby on rails] Implementation of like function

Introduction

Implemented a like function so that other users can like the posting app. Suppose you have a users table, a posts table, and a likes table.

association

First, consider the relationship between each table and define the association. ** User (1): Likes (many) ** ** Post (1): Like (many) ** I want to make one ** like ** per person per post, so I will also validate it.

like.rb


class Like < ApplicationRecord
  belongs_to :user
  belongs_to :post

  validates_uniqueness_of :post_id, scope: :user_id

end

If the post is deleted ** Like ** is also deleted.

post.rb


has_many :likes, dependent: :destroy

user.rb


has_many :likes, dependent: :destroy

def already_liked?(post)
  self.likes.exists?(post_id: post.id)
end

Controller implementation

likes_controller.rb


class LikesController < ApplicationController

  def create
    @like = current_user.likes.create(post_id: params[:post_id])
    redirect_back(fallback_location: root_path)
  end

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

end

Routing settings

routes.rb


resources :posts do
  resource :likes, only: [:create, :destroy]
end

post_likes DELETE /posts/:post_id/likes(.:format) likes#destroy POST /posts/:post_id/likes(.:format) likes#create

view implementation

~.html.erb


<% if current_user.already_liked?(post) %>
  <%= link_to post_likes_path(post), method: :delete do %>
    <i class="fas fa-heart"></i>
  <% end %>
<% else %>
  <%= link_to post_likes_path(post), method: :post do %>
    <i class="far fa-heart"></i>
  <% end %>
<% end %>
<%= post.likes.count %>    //Show the number of likes

Do you already like current_user? If it is true, it will be unliked, and if it is false, it will be liked.

Finally

There are other ways to implement likes, such as asynchronous implementation. I'm still studying, but I would like to increase what I can study various techniques. Thank you for reading to the end: grin:

Recommended Posts

[Ruby on rails] Implementation of like function
[Rails] Implementation of like function
Implementation of Ruby on Rails login function (Session)
[Rails] Asynchronous implementation of like function
[Rails] About implementation of like function
[Ruby on Rails] Comment function implementation
Ruby on Rails <2021> Implementation of simple login function (form_with)
Implementation of Ruby on Rails login function (devise edition)
[Ruby on Rails] Implementation of tagging function/tag filtering function
[Rails] Addition of Ruby On Rails comment function
[Ruby on Rails] Follow function implementation: Bidirectional
Implementation of like function (Ajax)
[Rails 6] Implementation of search function
[Rails] Implementation of category function
Ruby on Rails Email automatic sending function implementation
[Rails] Implementation of tutorial function
[Ruby on Rails] Asynchronous communication of posting function, ajax
Explanation of Ruby on rails for beginners ⑦ ~ Flash implementation ~
[Rails] Implementation of CSV import function
[Ruby on Rails] Introduced paging function
[Rails] Implementation of image preview function
[Rails] Implementation of user withdrawal function
[Rails] Implementation of CSV export function
[Ruby on Rails] CSV output function
[Ruby on Rails] DM, chat function
[Rails 6] Like function (synchronous → asynchronous) implementation
Implementation of like function in Java
[Ruby on Rails] Bookmark (favorite registration, like) function: One direction
A note about the seed function of Ruby on Rails
Rails sorting function implementation (displayed in order of number of like)
[Ruby on Rails] Introduction of initial data
[Ruby on Rails] Search function (not selected)
Let's summarize "MVC" of Ruby on Rails
part of the syntax of ruby ​​on rails
Rails [For beginners] Implementation of comment function
[Rails 6] Implementation of SNS (Twitter) sharing function
[Ruby on Rails] Japanese notation of errors
Explanation of Ruby on rails for beginners ①
[Vue.js] Implementation of menu function Implementation version rails6
[Vue.js] Implementation of menu function Vue.js introduction rails6
[Ruby on Rails] Logical deletion (withdrawal function)
Implementation of search function
Ruby on Rails basics
Rails search function implementation
Implementation of pagination function
Ruby On Rails Association
[Rails] Implementation of search function using gem's ransack
Validation settings for Ruby on Rails login function
[Rails 6] Implementation of inquiry function using Action Mailer
[Ruby on Rails] Until the introduction of RSpec
[Rails] Implementation of image enlargement function using lightbox2
[Rails] Implementation of retweet function in SNS application
Ruby on Rails ~ Basics of MVC and Router ~
[Ruby on Rails] A memorandum of layout templates
Ruby on Rails address automatic input implementation method
[Ruby on Rails] Post editing function (update, delete)
[Ruby on Rails] Individual display of error messages
Rails implementation of ajax removal
Rails fuzzy search function implementation
Ruby on rails learning record -2020.10.03
[Rails] Implementation of drag and drop function (with effect)