2016-04-04 20 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