【Rails6】비동기(Ajax)로 팔로우 기능을 구현한다
전제
개요
팔로우 기능의 구조에 대해서는, RailsTutorial에 상세하게 쓰여져 있으므로, 참조하는 것을 추천합니다. 이번에 사용하는 컬럼명이나 모델명은 다음과 같습니다. 메모 정도입니다만 참고 정도에 올려 둡니다.

Relationship 모델 만들기
콘솔에서 relationship 모델 만들기
rails g model relationships
콘솔에서 relationship 모델 만들기
rails g model relationships
followという名前で
user 테이블을 참조한다 20210104064312_create_relationships.rb
class CreateRelationships < ActiveRecord::Migration[6.0]
def change
create_table :relationships do |t|
t.references :user, foreign_key: true
t.references :follow, foreign_key: { to_table: :users }
t.timestamps
t.index [:user_id, :follow_id], unique: true
end
end
end
마이그레이션 실행
rails db:migrate
relationship 모델과 user 모델의 관계는 다음과 같이 기술한다.
또한 validation도 기술.
relationship.rb
class Relationship < ApplicationRecord
belongs_to :user
belongs_to :follow, class_name: 'User'
validates :user_id, presence: true
validates :follow_id, presence: true
end
relationship.rb
class User < ApplicationRecord
has_many :relationships, dependent: :destroy
has_many :followings, through: :relationships, source: :follow
has_many :reverse_of_relationships, class_name: 'Relationship', foreign_key: 'follow_id', dependent: :destroy
has_many :followers, through: :reverse_of_relationships, source: :user
def follow(other_user_id)
relationships.find_or_create_by(follow_id: other_user_id) unless id == other_user_id.to_i
end
def unfollow(other_user_id)
relationship = relationships.find_by(follow_id: other_user_id)
relationship.destroy if relationship
end
def following?(other_user)
followings.include?(other_user)
end
end
view controller 만들기
app/views/users/show.html.erb
<div>
<% if @user.id == current_user.id %>
<%= link_to 'Edit', edit_user_registration_path(current_user) %>
<% else %>
<%= render partial: 'follow' %>
<%= render partial: 'follow_count'%>
<% end %>
</div>
app/views/users/_follow.html.erb
<span id="follow">
<%= follow_button(@user) %>
</span>
remote: true
를 넣어 Ajax 통신을 할 수 있습니다. app/helpers/application_helper.html.erb
module ApplicationHelper
def follow_button(user)
label, key = current_user.following?(user) ? %w[フォロー中 secondary] : %w[フォローする primary]
link_to(label, user_follow_path(@user), method: :post, remote: true, class: "btn btn-#{key} rounded-pill")
end
end
html内のidがfollowの中身をrenderする
수 있습니다. app/views/users/follow.js.erb
$('#follow').html('<%= j(render partial: "follow") %>');
routes.rb
Rails.application.routes.draw do
resources :users, only: %w[index show] do
post :follow
end
end
마지막으로
@user.followers.count
또는 @user.followings.count
에서 팔로워 수와 팔로우 수를 계산하여 표시할 수 있습니다. 이 기사를 이해할 수 있다면 쉽게 Ajax로 구현할 수 있으므로 시도해보십시오.
Reference
이 문제에 관하여(【Rails6】비동기(Ajax)로 팔로우 기능을 구현한다), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/engineer_ikuzou/items/fc16edd2578bee41e17f
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
Reference
이 문제에 관하여(【Rails6】비동기(Ajax)로 팔로우 기능을 구현한다), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/engineer_ikuzou/items/fc16edd2578bee41e17f텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)