[RUBY] Follow function (Ajax) implementation

I will put it together for myself.

Implementation

Modeling

$ rails g model Relationship follower_id:integer followed_id:integer
$ rails db:migrate

Migration file

20200723070930_create_relationships.rb


class CreateRelationships < ActiveRecord::Migration[5.2]
  def change
    create_table :relationships do |t|
      t.integer :follower_id, null: false
      t.integer :followed_id, null: false

      t.timestamps
    end
  end
end

relationship.rb


class Relationship < ApplicationRecord

  #Users to follow
  belongs_to :follower, class_name: 'User'
  #Users to be followed
  belongs_to :followed, class_name: 'User'

  validates :follower_id, presence: true
  validates :following_id, presence: true

end

** class_name:'User' **: means "both follower and followed are in User"


app/models/user.rb


class User < ApplicationRecord

  #Get follow
  has_many :follower, class_name: 'Relationship', foreign_key: 'follower_id', dependent: :destroy

  #Get followers
  has_many :followed, class_name: 'Relationship', foreign_key: 'followed_id', dependent: :destroy

  #The person you are following
  has_many :following_user, through: :follower, source: :followed

  #People who follow me (followers)
  has_many :follower_user, through: :followed, source: :follower


  #Follow users
  def follow(user_id)
    follower.create(followed_id: user_id)
  end

  #Unfollow users
  def unfollow(user_id)
    follower.find_by(followed_id: user_id).destroy
  end

  #Follow confirmation
  def following?(user)
    following_user.include?(user)
  end

end

Controller creation

$ rails g controller relationships

config/routes.rb


Rails.application.routes.draw do

  post 'follow/:id', to: 'relationships#follow', as: 'follow'
  post 'unfollow/:id', to: 'relationships#unfollow', as: 'unfollow'
  get 'users/following/:user_id', to: 'users#following', as: 'users_following'
  get 'users/follower/:user_id', to: 'users#follower', as: 'users_follower'

end

users_controller.rb


class UsersController < ApplicationController

  # @List of users that user is following
  def following
    @user = User.find(params[:user_id])
    @followings = @user.following_user
  end

  # @List of users following user
  def follower
    @user = User.find(params[:user_id])
    @followers = @user.follower_user
  end

end

relationship_controller.rb


class RelationshipsController < ApplicationController

    #To follow
    def follow
      @user = User.find(params[:id])
      current_user.follow(params[:id])
      render :create
    end
  
    #Unfollow
    def unfollow
      @user = User.find(params[:id])
      current_user.unfollow(params[:id])
      render :destroy
    end
    
end

By calling create.js.erb and destroy.js.erb with render, You will be able to follow / unfollow without page transitions.


ruby:users/show.html.slim


# create.js.erb and destroy.js.Description is required to rewrite with erb
div id="follow_form"

  #Partially skip the follow button part
  = render 'relationships/follow', user: @user 

Manually create the _follow.html.slim file under the relationships directory Paste the description of the follow button part that was skipped in the partial.

ruby:relationships/_follow.html.slim


- unless user == current_user
  - if current_user.following?(user)
  
    #remote to call js file:Add true
    = link_to unfollow_path(user), method: :post, remote: true, class: 'btn btn-outline-info m-0 btn-sm' do
      i.fas.fa-user following
  - else
  
    #remote to call js file:Add true
    = link_to follow_path(user), method: :post, remote: true, class: 'btn btn-outline-success m-0 btn-sm' do
      i.far.fa-user follow

Manually create the create.js.erb file under the relationships directory

ruby:relationships/create.js.erb



# id="follow_form"Part_follow.html.Rewrite slim content without page transition
$('#follow_form').html("<%= j(render 'relationships/follow',{ user: @user }) %>");

Manually create the destroy.js.erb file under the relationships directory

ruby:relationships/destroy.js.erb



# id="follow_form"Part_follow.html.Rewrite slim content without page transition
$('#follow_form').html("<%= j(render 'relationships/follow',{ user: @user }) %>");

Complete!

Recommended Posts

Follow function (Ajax) implementation
Comment function (Ajax) implementation
Implementation of like function (Ajax)
DM function implementation
Rails follow function
[Ruby on Rails] Follow function implementation: Bidirectional
Where the follow function implementation is stuck
Implemented follow function in Rails (Ajax communication)
[Rails 6] Asynchronous (Ajax) follow function is implemented
Implementation of search function
Image preview function implementation
Rails search function implementation
Implementation of pagination function
Search function [copype implementation]
Implement user follow function in Rails (I use Ajax) ②
Implement user follow function in Rails (I use Ajax) ①
Rails implementation of ajax removal
Rails fuzzy search function implementation
Implementation of sequential search function
Use ransack Search function implementation
[Rails 6] Implementation of search function
Implementation of image preview function
[Rails] Implementation of category function
Implementation of category pull-down function
[Rails] (Supplement) Implemented follow function
Login function implementation with rails
Ajax bookmark function using Rails
[Rails] Implementation of tutorial function
[Rails] Implementation of like function
[Rails 6] Pagination function implementation (kaminari)
[Rails] Implementation of CSV import function
[Rails] Asynchronous implementation of like function
[Rails] Implementation of image preview function
[Rails] About implementation of like function
[Rails] Implementation of user withdrawal function
[Rails] Implementation of CSV export function
[Rails] gem ancestry category function implementation
[Ruby on Rails] Comment function implementation
[Rails 6] Like function (synchronous → asynchronous) implementation
[Rails] Comment function implementation procedure memo
Error encountered in tagging function implementation
Implementation of like function in Java
[Rails] I will explain the implementation procedure of the follow function using form_with.
Implementation of user authentication function using devise (1)
Rails [For beginners] Implementation of comment function
Rails Basic CRUD function implementation procedure scaffold
[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
[Ruby on rails] Implementation of like function
[Vue.js] Implementation of menu function Vue.js introduction rails6
Login function implementation by Spring Security (securityConfig)