Rails 4多态关联和关注点

Mel*_*Mel 8 polymorphism ruby-on-rails polymorphic-associations

我正在尝试将Evaluation模型添加到我的Rails 4应用中.

我做了一个名为的模型evaluation.rb.它有:

class Evaluation < ActiveRecord::Base

  belongs_to :evaluator, :polymorphic => true
  belongs_to :evaluatable, :polymorphic => true
Run Code Online (Sandbox Code Playgroud)

我也提出了担忧evaluator,并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
Run Code Online (Sandbox Code Playgroud)

我在用户模型中包含了每个问题:

class User < ActiveRecord::Base
   include Evaluator
   include Evaluatable
Run Code Online (Sandbox Code Playgroud)

在我的展示页面中,我想展示特定用户的评估(从其他用户 - 他们是评估者)收到.

在我的节目中,我有:

<% 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>
Run Code Online (Sandbox Code Playgroud)

在我的评估表中,我不确定如何指定评估的接收者.我已经制作了基本表格,但我不清楚如何将其与应该接受评估的用户联系起来.

<%= 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}  %>
Run Code Online (Sandbox Code Playgroud)

我的评估表有:

    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
Run Code Online (Sandbox Code Playgroud)

质询

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

如何调整表单以便将用户ID指定为应该接收评估的人?

Mar*_*nez 3

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

您对模型的关注应该对您有所帮助。在您的UsersController#show操作中,只需添加以下内容即可解决问题:

@received_evaluations = @user.received_evaluations
Run Code Online (Sandbox Code Playgroud)

然后您可以在您的展示模板中使用它:

<% @received_evaluations.each do |evaluation| %>
  // render some view stuff
<% end %>
Run Code Online (Sandbox Code Playgroud)

或者使用集合渲染

注意:Evaluation.find(...)当前在您视图中的内容应该放在控制器操作中,将其保留在视图中并不是一个好的做法。

如何调整表单,以便将用户 ID 指定为应接受评估的人员?

如果您已经确定了将充当的用户,则evaluatable可以在控制器操作或视图表单中进行设置,以防您的页面上有要评估的用户列表。

在控制器中:

@evaluation.evaluatable_id = user_to_evaluate.id
@evaluation.evaluatable_type = user_to_evaluate.class.to_s
Run Code Online (Sandbox Code Playgroud)

或者这个更简单的语句应该做同样的事情:

@evaluation.evaluatable = user_to_evaluate
Run Code Online (Sandbox Code Playgroud)

同样,您应该能够以相同的方式设置评估器:

@evaluation.evaluator = user_that_evaluates
Run Code Online (Sandbox Code Playgroud)

视图中:

<% @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 %>
Run Code Online (Sandbox Code Playgroud)