2016-04-04 107 views
8

我正在嘗試將Evaluation模型添加到我的Rails 4應用中。Rails 4多態關聯和關注

我製作了一個名爲evaluation.rb的模型。它具有:

evaluator
class Evaluation < ActiveRecord::Base 

    belongs_to :evaluator, :polymorphic => true 
    belongs_to :evaluatable, :polymorphic => true 

我也讓關注和evaluatable爲:

module Evaluator 
    extend ActiveSupport::Concern 

    included do 
     has_many :given_evaluations, as: :evaluator, dependent: :destroy, class_name: 'Evaluation' 

    end 
end 

module Evaluatable 
    extend ActiveSupport::Concern 

    included do 
     has_many :received_evaluations, as: :evaluatable, dependent: :destroy, class_name: 'Evaluation' 
    end 
end 

我已經包含在我的用戶模型中的每個關注:

class User < ActiveRecord::Base 
    include Evaluator 
    include Evaluatable 

在我的節目頁,我想顯示特定用戶的評估(從其他用戶(評估人員)接收)。

在我的節目,我有:

<% Evaluation.find(params[:id]).evaluations.order('created_at DESC').each do |eval| %> 
           <div id="portfolioFiltering" class="masonry-wrapper row"> 
             <%= eval.remark %> 
             <%= eval.personal_score %> 
             <small><%= eval.created_at %></small> 

在我的評價形式,我「不知道如何指定評估的收件人我已經基本形成,但我不清楚關於如何將它與誰應該接受評估用戶

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

    <div class="form-inputs"> 
    <%= f.input :score, collection: 1..10, autofocus: true, :label => "How do you rate this experience (1 being did not meet expectations - 10 being met all expectations) ?" %> 

    <%= f.input :remark, as: :text, :label => "Evaluate your project experience", :input_html => {:rows => 10} %> 

我的評價表有:

t.integer "user_id" 
    t.integer "evaluatable_id" 
    t.string "evaluatable_type" 
    t.integer "overall_score" 
    t.integer "project_score" 
    t.integer "personal_score" 
    t.text  "remark" 
    t.boolean "work_again?" 
    t.boolean "continue_project?" 
    t.datetime "created_at",  null: false 
    t.datetime "updated_at",  null: false 
    end 

    add_index "evaluations", ["evaluatable_type", "evaluatable_id"], name: "index_evaluations_on_evaluatable_type_and_evaluatable_id", unique: true, using: :btree 

問題

如何設置顯示頁面以顯示用戶收到的評估?

我該如何調整表格,使其指定用戶ID作爲應接受評估的人員?

+0

偉大的問題,你很擅長解釋你的問題..讓我們看看是否有幫助 – illusionist

+0

你打算使用'評估'評估用戶以外的對象?根據你的回覆,答案可以簡化。 –

+0

是的。我想評估用戶做的項目 – Mel

回答

2

如何設置顯示頁面以顯示用戶收到的評估?

你的模型問題應該幫助你。在你UsersController#show動作,只需添加以下應該做的伎倆:

@received_evaluations = @user.received_evaluations 

然後你就可以在你的節目模板中使用它:

<% @received_evaluations.each do |evaluation| %> 
    // render some view stuff 
<% end %> 

或者使用collection rendering

注意:當前在您的視圖中的Evaluation.find(...)應該放在控制器操作中,將它留在視圖中不是很好的做法。

我該如何調整表格,使其指定用戶ID作爲應接受評估的人員?

如果您確定將用作evaluatable的用戶,則可以將其設置爲您的控制器操作或您的查看錶單,以防您有要在您的頁面上評估的用戶列表。

在控制器:

@evaluation.evaluatable_id = user_to_evaluate.id 
@evaluation.evaluatable_type = user_to_evaluate.class.to_s 

還是這個簡單的說法也應該這樣做:

@evaluation.evaluatable = user_to_evaluate 

同樣的,你應該能夠設置評價者以同樣的方式:

@evaluation.evaluator = user_that_evaluates 

查看:

<% @users_to_evaluate.each do |user| %> 
    <%= simple_form_for(Evaluation.new) do |f| %> 
    <%= f.error_notification %> 

    <div class="form-inputs"> 
     <%= f.input :score, collection: 1..10, autofocus: true, :label => "How do you rate this experience (1 being did not meet expectations - 10 being met all expectations) ?" %> 
     <%= f.input :remark, as: :text, :label => "Evaluate your project experience", :input_html => {:rows => 10} %> 
     <%= f.hidden_field :evaluator_id, :value => current_user.id %> 
     <%= f.hidden_field :evaluator_type, :value => current_user.class.to_s %> 
     <%= f.hidden_field :evaluatable_id, :value => user.id %> 
     <%= f.hidden_field :evaluatable_type, :value => user.class.to_s %> 
    </div> 
    <% end %> 
<% end %> 
+0

好吧!非常感謝Marc。我今晚會試一試。 – Mel