Follow Unfollow Feature like social media using rails

Total Blog Views: 52

Blog Status: publish

Created By: asad_fazlani Created at: 08-31-2021

Tags: ruby rails rubyonrails social media follow/unfollow rails follow unfollow like instagram ruby on rails

Most common feature in any social media platform for engagement is the users follow each other. so in this blog i share step by step how to develop follow unfollow feature in rails app.

first create rails app using command rails new app_name. 

after creating app add device gem in your app refer this click here

Now, you need to create follow unfollow model  run below command

rails g resource follows follower_id:integer followee_id:integer
Add below code in Follow.rb 
 
class Follow < ApplicationRecord
belongs_to :follower, class_name: 'User'
belongs_to :followee, class_name: 'User'
validates :follower_id, uniqueness: { scope: :followee_id }
validates :followee_id, uniqueness: { scope: :follower_id }
end

Add below code in user.rb

class User < ApplicationRecord
  devise :database_authenticatable, :registerable,
  :recoverable, :rememberable, :validatable
  has_many :followed_users, foreign_key: :follower_id, class_name: 'Follow'
  has_many :followees, through: :followed_users
  has_many :following_users, foreign_key: :followee_id, class_name: 'Follow'
  has_many :followers, through: :following_users
  validates :user_name, uniqueness: { case_sensitive: true }
  validates :user_name, presence: true
  validates :name, presence: true
  validates :location, presence: true
  validates :bio, presence: true
end

Add below code in user_controller.rb

class UsersController < ApplicationController
before_action :set_user,only: [:show, :edit, :destroy, :update]
def index
@users = User.all
end 
def show
end
def follow
@user = User.find(params[:id])
current_user.followees << @user
redirect_back(fallback_location: user_path(@user))
end
def unfollow
@user = User.find(params[:id])
current_user.followed_users.find_by(followee_id: @user.id).destroy
redirect_back(fallback_location: user_path(@user))
end
def set_user
@user = User.find(params[:id])
end
end

Add routes in routes.rb

Rails.application.routes.draw do
  root 'users#index'
  resources :follows
  devise_for :users
  resources :users
  post '/users/:id/follow', to: "users#follow", as: "follow_user"
  post '/users/:id/unfollow', to: "users#unfollow", as: "unfollow_user"
end

Add in you Design page

<% @users.each do |user| %>
<div>
<h1><%=link_to user.email ,user %></h1>
<% if current_user.present? %>
<section id="all-links">
<% if current_user.present? %>
<% if current_user == user %>
<% elsif current_user.followees.include?(user) %>
<h5><%= button_to "UnFollow",     
unfollow_user_path(user.id), method: "POST", class:"btn btn-primary" %></h5>
<% else %>
<h5><%= button_to "Follow", follow_user_path(user.id), 
method: "POST", class:"btn btn-primary"  %></h5>
<% end %>
<% end %>
<br>
</section>
<% end %>
</div>
<% end %>
The copmplete Working demo in Link
The complete Code is in the link
Thanks for reading,


asad_fazlani

Technical Blogger at shadbox



Comments



  • swazahmad | almost 4 years ago
    Really useful Information
  • vishnu | almost 4 years ago
    Bravo! Information is Fantastic ☺️
Buy traffic for your website

About Shadbox

we have the “Get things executed” lifestyle at our place of work. There are not any excuses, no if’s or however’s in our dictionary. committed to navigating the ship of creativity to create cell answers, we resolve the real-lifestyles troubles of our clients and their clients. Our passion for work has won us many awards, year after 12 months.

Services

Downloads