[RUBY] Rails follow function

① Model (3 steps)

○ Create the required models (2)

  1. USER (created with devise)  2. Relationships   rails g model Relationship follower_id:integer followed_id:integer

    create_table "relationships", force: :cascade do |t| t.integer "follower_id" t.integer "followed_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false end

○ Association

Added to each model.

Relationship model [Relationships.rb]

belongs_to :follower, class_name: "User"
belongs_to :followed, class_name: "User"

User model [User.rb]

#Following has_many :follower, class_name: "Relationship", foreign_key: "follower_id", dependent: :destroy

Followed

    has_many :followed, class_name: "Relationship", foreign_key: "followed_id", dependent: :destroy

#People who follow has_many :follower_user, through: :followed, source: :follower #People being followed has_many :following_user, through: :follower, source: :followed

: point_up: Relationship model can also be divided into "follower" and "followed" By adding: point_up: foreign_key, each can be obtained through the Relationship model.

: point_up: Through each source model

○ Method definition (3) [User.rb]

1. follow method = follow

  def follow(user_id)
   follower.create(followed_id: user_id)
  end

2. unfollow method = unfollow

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

3. following method = Confirm if you are already following

  def following?(user)
   following_user.include?(user)
  end

It is used when conditional branching of display with: point_up: html. If user is included in the argument, return true = If true, "Unfollow" is displayed.

②Controller

○ Method definition: Relationships Controller

def create # follow method defined in the User model to follow current_user.follow(params[:user_id]) redirect_to request.referer # Get the URL before the transition and redirect end

def destroy # unfollow unfollow method defined in User model current_user.unfollow(params[:user_id]) redirect_back(fallback_location: root_path) end

def follower #follower list user = User.find(params[:user_id]) @users = user.following_user

.follower_user method: Defined in User model

end

def followed #followed list user = User.find(params[:user_id]) @users = user.follower_user

.follower_user method: Defined in User model

end

③Routing

Added for list of create (follow), destroy (unfollow) and follows, followers

resources :users
 resource :relationships, only:[:create, :destroy]

 get 'follows' => 'relationships#follower'
 get 'followers' => 'relationships#followed'
end

④View

・ Follow button (follow / unfollow)

Add a link to the part where you want to display the follow button

 <% if current_user != user %>
  <% if current_user.following?(user) %>

<% = link_to'Unfollow', user_relationships_path (user.id), method:: delete, class: "btn btn-default"%> <% else %> <% = link_to'Follow', user_relationships_path (user.id), method :: POST, class: "btn btn-primary"%> <% end %> <% end %>

・ Follower.follows list

Create follower.html.erb and follows.html.erb files in relationships Described in each file as follows

If there is a User in the conditional branch of <% if @ users.count> 0%>, display it in each statement and display it. If not, ** No user ** is displayed

 <% if @users.count > 0 %>
   <table class="table">
    <thead>
      <tr>
        <th>name</th>
       </tr>
     </thead>
     <tbody>
       <% @users.each do |user| %>
        <tr>
           <td><%= @user.name %></td>
Number of followers: <% = @ user.follower.count%> Number of followers: <% = @ user.followed.count%>  <% if current_user != @user %>  <% if current_user.following?(@user) %> <% = link_to'Unfollow', user_relationships_path (@ user.id), method:: delete, class: "btn btn-default"%> <% else %> <% = link_to'Follow', user_relationships_path (@ user.id), method :: POST, class: "btn btn-primary"%> <% end %> <% end %> <%= link_to "Show", @user %> <% end %> <% else %>

No users <% end %>

Recommended Posts

Rails follow function
Implement follow function in Rails
[Rails] (Supplement) Implemented follow function
[Rails 6] Ranking function
[Rails] Category function
[Rails] Notification function
[Ruby on Rails] Follow function implementation: Bidirectional
Implemented follow function in Rails (Ajax communication)
[Rails 6] Asynchronous (Ajax) follow function is implemented
Follow function (Ajax) implementation
[Rails] Implement search function
[Rails] Implemented hashtag function
[rails] tag ranking function
Rails search function implementation
How to make a follow function in Rails
Implement application function in Rails
Rails fuzzy search function implementation
[Rails] Implement User search function
Introduced graph function with rails
Implement user follow function in Rails (I use Ajax) ②
[Rails 6] Implementation of search function
[Rails] Implementation of category function
Rails ~ Understanding the message function ~
Implement user follow function in Rails (I use Ajax) ①
Login function implementation with rails
[Rails] EC site cart function
Ajax bookmark function using Rails
[Rails] Implementation of tutorial function
[Rails] Implement image posting function
[Rails] Implementation of like function
[Rails 6] Pagination function implementation (kaminari)
[Rails] Implementation of CSV import function
[Rails] Asynchronous implementation of like function
Add a search function in Rails.
[Rails] About the Punk List function
[Ruby on Rails] Introduced paging function
[Rails] Implementation of image preview function
Implemented mail sending function with rails
Kaminari --Added pagination function of Rails
[Rails] About implementation of like function
[Rails] Implementation of user withdrawal function
[Rails] Implementation of CSV export function
Create pagination function with Rails Kaminari
[Ruby on Rails] CSV output function
[Rails] Voice posting function ~ Cloudinary, CarrierWave
[Rails] Comment function (registration / display / deletion)
[Rails] gem ancestry category function implementation
[Ruby on Rails] Comment function implementation
[Ruby on Rails] DM, chat function
[Rails 6] Like function (synchronous → asynchronous) implementation
Implement CSV download function in Rails
[Rails] Comment function implementation procedure memo
[Rails g.error]
[Ruby on Rails] Search function (not selected)
[Rails] Addition of Ruby On Rails comment function
Rails basics
[Rails] Create an evaluation function using raty.js
[Rails] Function restrictions in devise (login / logout)
Rails API
Rails migration
[Rails] first_or_initialize