現在ある従業員が他の従業員へ評価をつけるシステムをruby on rails で作っています。
一人のuserが他のuserの評価をラジオボタンでつけDBに保存されるところまでは行き評価した側のuser_idを保存することはできたのですが評価された相手のuser_idを評価テーブル(evaluation)に保存する方法がわからないです。
user_idが2つになってしまうのでその問題をどう解決したらいいか教えてもらえると助かります。
現状してはrandメソッドでuserの情報を@avaterに入れています、そこに評価されたuser_idをform_forで取ってこようと思っています。
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 @evaluation = Evaluation.new(create_params) # binding.pry @evaluation.user = current_user @evaluation.save redirect_to :root and return end private def create_params params.require(:evaluation).permit(:user_id, :rate) end end
users_controller.rb
class UsersController < ApplicationController def show @user = User.find(params[:id]) # binding.pry end def edit end def update 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
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(@evaluation)do |f| %> <div class="field"> <div class="profile_edit"> <%= f.radio_button :rate, :so_good, checked: "checked" %> <%= f.label :so_good %> </div> <div class="profile_edit"> <%= f.radio_button :rate, :good %> <%= f.label :good %> </div> <div class="profile_edit"> <%= f.radio_button :rate, :bad %> <%= f.label :bad %> </div> <div class="profile_edit"> <%= f.radio_button :rate, :so_bad %> <%= f.label :so_bad %> </div> </div> <%= f.submit "評価する" %> <% end %>
routes.rb
Rails.application.routes.draw do devise_for :users resources :evaluations, only: [:index ,:create] resources :users, only: [:edit, :update] resources :users, only: :show do resources :evaluations, only: [:index ,:create] end root to: "top#index" 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.timestamps end end end
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/04/16 11:52