How To Add Like and Unlike Function In Rails.

How To Add Like and Unlike Function In Rails.

25 May 2021
0 Comments
How To Add Like and Unlike Function In Rails.

 

To add the Like & Unlike feature in Rails, the first to accomplish is to make sure that the Post feature is already created. This step-by-step guide will provide a quick introduction to adding like & unlike to the post.

 

Follow the below-mentioned steps to add the like feature:

 

Step 1: Create the controller and model

 

First we need to generate the model.

                        rails g model PostLike post:references user:references                           

           

Then, migrate the table.

                        rails db:migrate                                                                                                           

 

Finally, add the controller:

                        rails g controller postlikes                                                                                                      

Step 2: Add the create method to the controller.

      

     

Use voter_id as a foreign key from the user table. user_id can also be used as a foreign key.

 

Step 3: In app/views/posts/show.html.erb, add the below-specified code:

 

Step 4: In routes.rb, nest the comments resource inside the posts resource:

 

Step 5: Establish one-to-many relationships between the Posts and Users Model.

 

Add the code: - app/model/post.rb and app/model/user.rb

 

 

Follow the below code for count like feature:

 

Step 1: In app/views/posts/show.html.erb and app/views/posts/show.html.erb

 

add the above-mentioned line of code to display the number of likes a post has.

 

 

Stop people from liking the post more than once:

 

Step 1: Add a method to verify if the user has already liked a photo. Under private access specifier, add

   

                                                                                                                                            

Step 2: Update the create function, to avoid multiple likes on the post with the help of already_liked? method.

 

 

Adding the unlike feature:

 

Step 1: In the like controller, add a destroy method.

                                                                                                               

This method makes sure that the user doesn’t unlike when they haven’t liked the post in the first place.

 

Step 2: To delete the like on the post, first find the like. To find the specific like, add a find_like method.

                                                                       

Step 3: To make sure this method is called on the destroy method, put this line of code at the top of the controller.

 

Step 4: Add the unlike button/link and make sure it displays when the user clicks the like button on the post.

                                                                                                                                               

In app/views/posts/index.html.erb

a) Find the Post_like with voter_id and current user id.

b) If both the ids match, go to delete the link and create a like link.




Leave a comment: