現在ある従業員が他の従業員へ評価をつけるシステム(tinder風)をruby on rails で作っています。
機能的には全て完成したのですがビューで表示している画像に対して評価ボタンを押すと違うuser.idがDBに登録されてしまいます。
ちなみに評価する人のカラムがuser_idで評価される方のカラムがreviewer_idです
エラーは何も出ていないです。
自分的に思っている問題点としてはevaluations_controller.rbのindexとcreateで@avataerがとってきている情報が一致していないのが原因だと考えているのですが、そこを一致させる方法がわからないです。
ぜひ教えてもらえると助かります。
evaluations_controller.rb
class EvaluationsController < ApplicationController def index @evaluation = Evaluation.new @avatar1 = User.where.not(family_name: "田中" ).select(:family_name) @avatar = User.order("RAND()").first # binding.pry end def create @avatar = User.order("RAND()").first @evaluation = Evaluation.new(create_params) # binding.pry @evaluation.user = current_user @evaluation.save redirect_back(fallback_location: user_evaluations_path(current_user, @avatar)) end private def create_params params.require(:evaluation).permit(:rate).merge(reviewer_id: @avatar.id, user_id: current_user.id) end end
users_controller.rb
class UsersController < ApplicationController def show @user = User.find(params[:id]) # binding.pry end def edit @user = current_user end def update @user = current_user current_user.update(update_params) end private def update_params params.require(:user).permit(:family_name, :first_name, :family_name_kana, :first_name_kana, :avatar) end end
evaluationsの
index.html.erb
<div class="posting_form top_content"> <header class="posting_header"> <div class="row form user_profile"> <div class="col-lg-12"> <div class="image" style="background-image: url(<%= @avatar.avatar.url(:medium) %>);"></div> <div class="posting_user_info user_info"> <%= @avatar.family_name %> </div> </div> </div> <div class="col-lg-12"> </header> <%= form_for [@avatar, @evaluation] do |f| %> <div class="field"> <div class="profile_edit"> <%= hidden_field_tag :reviewer_id, @avatar.id %> <%= f.radio_button :rate, :so_good, checked: "checked" %> <%= f.label :so_good %> </div> <div class="profile_edit"> <%= hidden_field_tag :reviewer_id, @avatar.id %> <%= f.radio_button :rate, :good %> <%= f.label :good %> </div> <div class="profile_edit"> <%= hidden_field_tag :reviewer_id, @avatar.id %> <%= f.radio_button :rate, :bad %> <%= f.label :bad %> </div> <div class="profile_edit"> <%= hidden_field_tag :reviewer_id, @avatar.id %> <%= f.radio_button :rate, :so_bad %> <%= f.label :so_bad %> </div> </div> <%= f.submit "評価する" %> <% end %>
create_evaluations.rb
class CreateEvaluations < ActiveRecord::Migration[5.0] def change create_table :evaluations do |t| t.integer :user_id t.integer :rate t.integer :reviewer_id t.timestamps end end end
evaluation.rb
class Evaluation < ApplicationRecord belongs_to :user belongs_to :reviewer, class_name: 'User', foreign_key: :reviewer_id validates_presence_of :user_id, :rate ,:reviewer_id enum rate: {so_good:1, good:2, bad:3 ,so_bad:4} end
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。