Open the home screen of another user you haven't followed yet and press the follow button that appears.
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.
Open the home screen of the user you are already following and press the unfollow button that is displayed.
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