LoginSignup
0
0

More than 1 year has passed since last update.

星評価(raty)の平均値を表示する

Posted at

参考
https://qiita.com/busitora2/items/5b59d1ea9e90c1b016b4

前提

・Userモデル
・Movieモデル
・Commentモデル

MovieモデルとUserモデルは多対多の関係です。(commentが中間テーブル)

自分の場合は星評価はすでに機能として付けていたので、平均値を表示するところからです。

commentsテーブルのデータ型はこんな感じです。

2023***create_comments.rb
class CreateComments < ActiveRecord::Migration[6.0]
  def change
    create_table :comments do |t|
      t.text :content
      t.references :user, foreign_key: true, null: false
      t.references :movie, foreign_key: true, null: false
      t.integer :likes_count, default: 0
      t.float :raty, default: 1.0, null: false

      t.timestamps
    end
  end
end
movies_controller.rb
def show
    @movie = Movie.find(params[:id])
    @comment = Comment.new
    @comments = @movie.comments
    @user = User.find_by(id: @comment.user_id) 
  end
comments_controller.rb
class CommentsController < ApplicationController
  before_action :logged_in_user, only: [:create, :destroy]
  before_action :correct_user,   only: :destroy

  def create
    @comment = current_user.comments.new(comment_params) 
    if @comment.save
      flash[:success] = 'レビューを投稿しました。'
      redirect_to movie_path(@comment.movie_id)
    end
  end

  def destroy
    @comment.destroy
    flash[:success] = 'レビューを削除しました。'
    redirect_back(fallback_location: root_path)
  end

  private
    def correct_user
      @comment = current_user.comments.find_by(id: params[:id])
      redirect_back fallback_location: root_path if @comment.nil?
    end
    
    def comment_params
      params.require(:comment).permit(:movie_id, :user_id, :content, :raty) 
    end
end

表示したいviewに書いていきます。

movies/show.html
<% if !@current_user_comment.nil? %>
    <h5>あなたのコメント・感想:</h5>
    <p><%= @current_user_comment.content %></p>
    <%= link_to "削除", @current_user_comment, method: :delete, data: { confirm: "本当に削除しますか?" } %>
<% else %>
    <%= form_for @comment do |f| %>
    <div class="field" id="star">
      <%= f.label :raty, "星評価:" %>
      <%= f.hidden_field :raty, id: :comment_star %>
    <script>
        $('#star').raty({
          starOff: "<%= asset_path('star-off.png') %>",
          starOn: "<%= asset_path('star-on.png') %>",
          starHalf: '<%= asset_path('star-half.png') %>',
          scoreName: 'comment[raty]',  <!-- ratyカラムへの保存 -->
          half: true,  <!-- 星半分の入力 -->
    });
    </script>
    </div>
      <%= f.hidden_field :movie_id, value: @movie.id %>
      <%= f.text_field :content, class: "text-field" %>
  
      <%= f.submit '投稿', class: "submit-btn" %>
    <% end %>
        <% end %>
//↑コメントと星評価の投稿

//↓星評価の平均値
      <div id="star-rate<%= @movie.id %>">
        <p>コメント<%= @movie.comments.count %></p>
      </div>   
    <script>
      $('#star-rate<%= @movie.id%>').raty({
        size      : 36,
        starOff   : '<%= asset_path('star-off.png') %>',
        starOn    : '<%= asset_path('star-on.png') %>',
        starHalf: '<%= asset_path('star-half.png') %>',
        half: true,
        readOnly: true, 
        score: <%= @movie.comments.average(:raty).to_f.round(1) %>,
        //↑ 平均点を算出し、round関数で切り上げる
      });
    </script>

    (省略)
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0