[RUBY] Rails tutorial (6th edition) Follow/unfollow background operation

table of contents

  1. Create new account
  2. Login (including Friendly Forwarding and permanent login) (https://qiita.com/akarin0519/items/f241b9699e156741a8d1)
  3. Edit Profile (https://qiita.com/akarin0519/items/4b3c64f88aefcaeb1781)
  4. Reset Password (https://qiita.com/akarin0519/items/ea873bb165ed4099a40e)
  5. Post Micropost
  6. ** Follow and unfollow **

6. Follow and unfollow

6.1 Follow

Operation screen

Open the home screen of another user you haven't followed yet and press the follow button that appears.

Background operation

First, make sure the user of the open page is not the current user (make sure params [: id] is different from session [: user_id] or cookies.signed [: user_id]). Then check if the user is already following the user on the open page (make sure params [: id] is currently in the user's following list) and still have to follow If so, display the follow button. In the input form displaying the follow button, the user's active_relationships instance is currently created and passed, so Rails executes the create action of the Relationshipchips controller based on the instance being empty when the follow button is pressed. Judge to do. At that time, the user ID of the open page is stored in params [: followed_id] and passed as a hidden field to the create action. This create action adds the passed params [: followed_id] to the current user's following array and completes the current user's follow.

6.2 Unfollow

Operation screen

Open the home screen of the user you are already following and press the unfollow button that is displayed.

Background operation

First, make sure the user of the open page is not the current user (make sure params [: id] is different from session [: user_id] or cookies.signed [: user_id]). Check if the user is already following the user on the open page (check if params [: id] is currently in the user's following list) and unfollow button if they are already following Is displayed. In the input form displaying the unfollow button, the Relationship instance (instance whose follower_id is the current user and the followed_id is the user of the page) searched based on the current user and the user ID of the page is passed. When the unfollow button is pressed, Rails will assume that the relationships controller destroy action (DELETE request to/relationships /: id) because the passed instance is not empty. In the destroy action, unfollowing is completed by extracting the user who was following from the ID of the Relationship instance received by params [: id] and deleting that user from the following user's following array.

Recommended Posts

Rails tutorial (6th edition) Follow/unfollow background operation
Rails tutorial (6th edition) Background operation of profile editing
Rails tutorial (6th edition) Background operation of login function
Rails tutorial (6th edition) Background operation of password reset function
Rails Tutorial (4th Edition) Summary
Rails Tutorial (6th Edition) -Explanation of background operation for each function-
Rails tutorial (6th edition) Background operation of the posting function of the micro post
Rails Tutorial (4th Edition) Memo Chapter 6
Rails Tutorial 6th Edition Learning Summary Chapter 10
Rails Tutorial 6th Edition Learning Summary Chapter 7
Rails Tutorial 6th Edition Learning Summary Chapter 4
Rails Tutorial 6th Edition Learning Summary Chapter 9
Rails Tutorial 6th Edition Learning Summary Chapter 6
Rails Tutorial 6th Edition Learning Summary Chapter 5
Rails Tutorial 6th Edition Learning Summary Chapter 2
Rails Tutorial 6th Edition Learning Summary Chapter 3
Rails Tutorial 6th Edition Learning Summary Chapter 8
Rails Tutorial 4th Edition: Chapter 1 From Zero to Deployment
rails tutorial
rails tutorial
rails tutorial
rails tutorial
rails tutorial
rails tutorial
rails tutorial Chapter 6
Rails tutorial test
rails tutorial Chapter 1
Rails tutorial memorandum 1
Rails tutorial memorandum 2
rails tutorial Chapter 7
rails tutorial Chapter 5
rails tutorial Chapter 10
rails tutorial Chapter 9
rails tutorial Chapter 8
Start Rails Tutorial
Rails Tutorial Chapter 5 Notes
Rails Tutorial Chapter 10 Notes
Rails Tutorial Chapter 3 Notes
Rails Tutorial Chapter 3 Learning
[Rails] Learning with Rails tutorial
Rails Tutorial Memorandum (Chapter 3, 3.1)
Rails Tutorial Chapter 4 Notes
Rails Tutorial Chapter 4 Learning
Rails Tutorial Chapter 1 Learning
Rails Tutorial Chapter 2 Learning
Rails Tutorial Chapter 8 Notes
rails tutorial fighting notes Ⅲ
Rails Tutorial Memorandum (Chapter 3, 3.3.2)
[For beginners] Build the environment for Ruby on Rails Tutorial 4th Edition (Rails 5.1) in less than an hour!