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.
** 1. To implement the Like function, first create a Like table (intermediate table) to store UserID and IdeaID. ** **
$ rails g model like post:references user:references $ rails db:migrate
** 2. Associate user model, idea model, and like model respectively. ** **
class User < ApplicationRecord #Omission has_many :likes end
class Idea < ApplicationRecord #Omission has_many :likes end
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. ** **
$ rails g controller likes
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. ** **
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. ** **
<!--Omission--> <div> <%= button_to 'Good', idea_likes_path(@idea), class:'good-btn' %> </div> <!--Omission-->
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.
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. ** **
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. ** **
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. ** **
<div class="good-button"><%= @likes_count%></div> <!--Good rating--> <div class="bad-button2"><%= @dislikes_count%></div> <!--Bad evaluation-->
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.
In implementing this time, I was able to learn the intermediate table etc. again, so I think it was a good review.
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/