2016-06-12 83 views
0

我在努力弄清楚如何設置我的rails評估模型,以便用戶可以使用它來爲其他用戶留下反饋。Rails - 協會,用戶對用戶的反饋 - 顯示反饋

概述了我的問題在這個職位的關鍵部分:

Rails - feedback on specific users, how to set up the form to identify relevant users

我從收到的建議是,建立模型如下:

User.rb

has_many :given_evaluations, foreign_key: :evaluator_id, dependent: :destroy, class_name: Evaluation 
    has_many :received_evaluations, foreign_key: :evaluatee_id, dependent: :destroy, class_name: Evaluation 

評估.rb

belongs_to :evaluator, foreign_key: :evaluator_id, class_name: User 
    belongs_to :evaluatee, foreign_key: :evaluatee_id, class_name: User 

評價控制器

class EvaluationsController < ApplicationController 
    before_action :set_evaluation, only: [:show, :edit, :update, :destroy] 
    # before_filter :get_user, only: [:show, :edit, :update, :destroy] 

    # GET /evaluations 
    # GET /evaluations.json 
    def index 
    # @evaluations = Evaluation.all 
    @given_evaluations = current_user.given_evaluations 
    @received_evaluations = current_user.received_evaluations 
    end 




    # GET /evaluations/1 
    # GET /evaluations/1.json 
    def show 
    # @received_evaluations = @user.received_evaluations 
    @evaluation = current_user.received_evaluations.find_by(id: params[:id]) || current_user.given_evaluations.find(params[:id]) 

    # @received_evaluation = current_user.received_evaluations.find params[:id] 
    end 

    # GET /evaluations/new 
    def new 
    @evaluation = Evaluation.new 
    end 

    # GET /evaluations/1/edit 
    def edit 
    end 

    # POST /evaluations 
    # POST /evaluations.json 
    def create 
    # @evaluation = Evaluation.new(evaluation_params) 
    @evaluation = current_user.given_evaluations.build(evaluation_params) 

    respond_to do |format| 
     if @evaluation.save 
     format.html { redirect_to @evaluation, notice: 'Evaluation was successfully created.' } 
     format.json { render :show, status: :created, location: @evaluation } 
     else 
     format.html { render :new } 
     format.json { render json: @evaluation.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # PATCH/PUT /evaluations/1 
    # PATCH/PUT /evaluations/1.json 
    def update 
    current_user.given_evaluations.find(params[:id]) 
    respond_to do |format| 
     if @evaluation.update(evaluation_params) 
     format.html { redirect_to @evaluation, notice: 'Evaluation was successfully updated.' } 
     format.json { render :show, status: :ok, location: @evaluation } 
     else 
     format.html { render :edit } 
     format.json { render json: @evaluation.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # DELETE /evaluations/1 
    # DELETE /evaluations/1.json 
    def destroy 
    current_user.given_evaluations.find(params[:id]) 
    @evaluation.destroy 
    respond_to do |format| 
     format.html { redirect_to evaluations_url, notice: 'Evaluation was successfully destroyed.' } 
     format.json { head :no_content } 
    end 
    end 

    private 
    # Use callbacks to share common setup or constraints between actions. 
    def set_evaluation 
     @evaluation = Evaluation.find(params[:id]) 
    end 

    # Never trust parameters from the scary internet, only allow the white list through. 
    def evaluation_params 
     params[:evaluation].permit(:overall_score, :project_score, :personal_score, :remark, :work_again?, :continue_project?, :evaluatee_id) 
    end 
end 

評價形式

<%= simple_form_for(@evaluation) do |f| %> 
    <%= f.error_notification %> 

    <div class="form-inputs"> 
    <%= f.select :evaluatee_id, User.all.map{|u| [u.formal_name, u.id]} %> 

    <%= f.input :overall_score, collection: 1..10, autofocus: true, :label => "How do you rate this project experience (1 being did not meet expectations - 10 being met all expectations) ?" %> 
    <%= f.input :continue_project?, as: :boolean, checked_value: true, unchecked_value: false, :label => "Do you intend to continue working on the project?" %> 
    <%= f.input :remark, as: :text, :label => "Evaluate your experience", :input_html => {:rows => 10} %> 



    </div> 

    <div class="form-actions"> 
    <%= f.button :submit %> 

評價放映視圖

<% @received_evaluations.each do |receval| %> 
           <div id="portfolioFiltering" class="masonry-wrapper row"> 
             <%= receval.remark %> 
             <%#= eval.personal_score %> 
             <small><%= receval.created_at %></small> 
           </div>  
          <% end %> 
在評價放映視圖

替代嘗試

<% @given_evaluations.each do |receval| %> 
           <div id="portfolioFiltering" class="masonry-wrapper row"> 
             <%= receval.remark %> 
             <%#= eval.personal_score %> 
             <small><%= receval.created_at %></small> 
           </div>  
          <% end %> 

我現在遇到的問題是,無論我是否儘量展現在表演給出評價或接收的估計,我得到一個錯誤,指出消息:

undefined method `each' for nil:NilClass 

我無法弄清楚如何設置模型,以便用戶可以評估其他用戶。我想在各自的展示頁面上顯示每個用戶的評估結果。我無法弄清楚發生了什麼問題。我可以從控制檯看到用戶已收到評估結果如下:

=> #<Evaluation id: 8, evaluatee_id: 34, overall_score: 4, project_score: 5, personal_score: 5, remark: "jhjkhjhjkhkjhjkhjhkhjhkj", work_again?: nil, continue_project?: nil, created_at: "2016-06-12 21:52:53", updated_at: "2016-06-12 21:52:53", evaluator_id: 34> 

有一個條目供我正在使用的用戶使用。但是,我找不到一種方式來展示評估。

回答

0

的最直接的問題,我可以看到的是,實例變量:@given_evaluations@received_evaluations沒有在你的表演動作設定和評論部分使用ActiveRecord#find方法,它會返回一個實例因此原因,你不能循環評估。

我認爲在index行動中顯示您所有用戶的評價的更好的地方,就像您已經在做的那樣,您可以將當前顯示的邏輯移動到索引視圖。 要顯示在show動作每個用戶接收的評估,你會怎麼做:

@evaluation = current_user.received_evaluations.find(params[:id])

show視圖

然後,你應該是這樣的:

<div id="portfolioFiltering" class="masonry-wrapper row"> 
    <%= @evaluation.remark %> 
    <%= @evaluation.personal_score %> 
    <small><%= @evaluation.created_at %></small> 
</div>