[RAILS] User evaluation using the like function

Introduction

In the production of Oriap, I added a purchase function so that the details of the product can only be seen after purchase. Therefore, it is necessary to have a function that can be a decisive factor in the purchase of the buying side user by making it possible to see the evaluation of the selling side user. This time, we implemented it so as to give a user evaluation (** Good ** or ** Bad **) by using the like function for the product.

Conditions that have been completed by the time of implementation

  1. The devise gem has already been introduced and the contents are also described.
  2. The product is defined as idea, and the idea model and idea controller have been created and the contents are also described.
  3. User and idea routing settings, associations, etc. have been completed.

1. Implementation of like function (Good evaluation)

** 1. To implement the Like function, first create a Like table (intermediate table) to store UserID and IdeaID. ** **

Terminal

$ rails g model like post:references user:references
$ rails db:migrate

** 2. Associate user model, idea model, and like model respectively. ** **

user model


class User < ApplicationRecord
#Omission
  has_many :likes
end

idea model


class Idea < ApplicationRecord
#Omission
  has_many :likes
end

like model


class Like < ApplicationRecord
  belongs_to :user
  belongs_to :idea
end

** 3. Create a like controller.
Since we only save likes this time, define only the create action in the like controller. ** **

Terminal

$ rails g controller likes

like controller


class LikesController < ApplicationController
  def create
    Like.create(user_id: current_user.id,idea_id: params[:idea_id])
    redirect_to root_path
  end
end

** 4. Set the routing.
like is related to idea, so nest it in idea and set it. ** **

routes.rb


Rails.application.routes.draw do
#Omission
  resources :ideas,except: :index do
    resources :likes, only: :create
    #Omission
  end
#Omission
end

** 5. Describe so that the like button (Good button) is displayed in the view file. ** **

html:views/ideas/show.html.erb


<!--Omission-->
<div>
 <%= button_to 'Good', idea_likes_path(@idea), class:'good-btn' %>
</div>
<!--Omission-->

2. Implementation of like function (Bad evaluation)

Replace ** like ** in the implementation of the like function (Good evaluation) with ** dislike **, and repeat the same process.

I was able to additionally display the Good button and Bad button as shown in the image below on the screen after purchasing the product.

3. Display of user evaluation (Good Bad)

Since the purpose of this time is to implement a function to understand the evaluation of the user who sells the product, the ** Good or Bad total ** attached to all the products of that user is acquired and displayed. There is a need to.
Since we want to display the user evaluation on the user page, we will describe it in the show action.

** 1. Get the idea associated with User with User controller. ** **

User controller


class UsersController < ApplicationController
  def show
    @user = User.find(params[:id])
    @user_ideas= @user.ideas
  end
end

** 2. Write the code to calculate the sum of the variable @likes_count and the variable @dislikes_count using the each method. ** **

User controller


class UsersController < ApplicationController
def show
    @user = User.find(params[:id])
    @user_ideas= @user.ideas
    #Calculate the total of Good
    @likes_count = 0
    @user_ideas.each do |idea|
      @likes_count += idea.likes.count
    end

  #Calculate the total of Bad
    @dislikes_count = 0
    @user_ideas.each do |idea|
      @dislikes_count += idea.dislikes.count
    end
  end
end

** 3. Describe @likes_count (Good evaluation) and @dislikes_count (Bad evaluation) in views/users/show.html.erb and display them. ** **

html:views/users/show.html.erb


<div class="good-button"><%= @likes_count%></div>    <!--Good rating-->
<div class="bad-button2"><%= @dislikes_count%></div>  <!--Bad evaluation-->

4. Implementation completed

It is now displayed on the user page as shown in the image below. The number of Goods and Bads is displayed, and you can see the user's rating.

5. Finally

In implementing this time, I was able to learn the intermediate table etc. again, so I think it was a good review.

6. Reference article

I implemented it referring to the following article. Thank you very much. https://qiita.com/nojinoji/items/2c66499848d882c31ffa https://freecamp.life/rails-favorite/ https://yumanoblog.com/rails-likes/

Recommended Posts

User evaluation using the like function
[Rails] Create an evaluation function using raty.js
Implementation of user authentication function using devise (2)
Creating a user authentication function using devise
Implementation of user authentication function using devise (1)
Implementation of user authentication function using devise (3)
Implement user edit / update function without using devise
Implement the star five function using the for statement
Implement the product category function using ancestry ① (Preparation)
How to implement the breadcrumb function using gretel
I tried using the Server Push function of Servlet 4.0
[Rails] Test of star evaluation function using Raty [Rspec]
About the function double-java
Let's introduce the credit card function using payjp (preparation)
Let's understand the function!
Create a login authentication screen using the session function
Template creation program when using the reminder function in slack
Follow function association memorandum (understand the description of the User model)
[Rails] I tried to implement "Like function" using rails and js
[Rails] Implement User search function
Search function using [rails] ransack
The API looks like this!
Implementation of like function (Ajax)
Rails ~ Understanding the message function ~
Implement category function using ancestory
Ajax bookmark function using Rails
SwiftUI-Display the map using MapKit
[Rails] Implementation of like function
About adding a like function
Like function The part that is stuck in making it asynchronous
[Implementation procedure] Create a user authentication function using sorcery in Rails
[Swift] How to implement the Twitter login function using Firebase UI ①
I tried using the cache function of Application Container Cloud Service
[Rails] [jQuery] Asynchronous like function implementation using remote: true and js.erb
Try using the two-point measurement function of Firebase Performance Monitoring. [Android]
[Swift] How to implement the Twitter login function using Firebase UI ②