Rails 4 多态关联和关注点

Rails 4 Polymorphic associations and concerns

我正在尝试将 Evaluation 模型添加到我的 Rails 4 应用程序。

我制作了一个名为evaluation.rb的模型。它有:

class Evaluation < ActiveRecord::Base

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

我也对 evaluatorevaluatable 表示担忧:

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}  %>

我的评价table有:

    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 作为应该接收评估的人?

如何设置显示页面以显示收到的用户评价?

您的模型问题应该可以帮助您。在您的 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 %>