简体   繁体   English

Rails 4多态关联和关注点

[英]Rails 4 Polymorphic associations and concerns

I'm trying to add an Evaluation model to my Rails 4 app. 我正在尝试将Evaluation模型添加到我的Rails 4应用程序中。

I have made a model called evaluation.rb . 我已经创建了一个名为evaluation.rb的模型。 It has: 它具有:

class Evaluation < ActiveRecord::Base

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

I have also made concerns for evaluator and evaluatable as: 我也提出了担忧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

I have included each concern in my user model: 我已经在用户模型中包括了每个关注点:

class User < ActiveRecord::Base
   include Evaluator
   include Evaluatable

In my show page, I want to show a particular user's evaluations (received from other users -who are evaluators). 在我的显示页面上,我想显示特定用户的评估(从其他用户那里收到-他们是评估者)。

In my show, I have: 在我的节目中,我有:

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

In my evaluations form, I"m not sure how to designate the recipient of the evaluation. I have made the basic form, but I'm not clear about how to tie it to the user who should receive the evaluation. 在我的评估表中,我不确定如何指定评估的接受者。我已经完成了基本表单,但不清楚如何将其与应接收评估的用户绑定。

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

My evaluations table has: 我的评估表具有:

    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

QUESTIONS 问题

How do I setup the show page to show a user's evaluations received? 如何设置显示页面以显示用户收到的评价?

How do I adapt the form so that it specifies a user id as the person who should receive the evaluation? 如何修改表格,以便将用户ID指定为应接收评估的人?

How do I setup the show page to show a user's evaluations received? 如何设置显示页面以显示用户收到的评价?

Your model concerns should help you with that. 您的模型问题应该可以帮助您。 In your UsersController#show action, simply adding the following should do the trick: 在您的UsersController#show动作中,只需添加以下内容即可解决问题:

@received_evaluations = @user.received_evaluations

Then you can use it in your show template: 然后,您可以在显示模板中使用它:

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

Or use collection rendering . 或使用集合渲染

note: the Evaluation.find(...) that's currently in your view should be put in the controller action, it's not good practice to leave that in the view. 注意:您当前在视图中使用的Evaluation.find(...)应该放在控制器动作中,将其留在视图中不是一种好习惯。

How do I adapt the form so that it specifies a user id as the person who should receive the evaluation? 如何修改表格,以便将用户ID指定为应接收评估的人?

If you have identified the user that will serve as evaluatable you could set it in your controller action or in your view form in case you have a list of users to evaluate on your page. 如果您确定了可以evaluatable的用户,则可以在控制器操作或视图表单中进行设置,以防页面上有要评估的用户列表。

In the controller: 在控制器中:

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

Or this simpler statement should do the same: 或更简单的语句应该做同样的事情:

@evaluation.evaluatable = user_to_evaluate

Similarly, you should be able to set the evaluator the same way: 同样,您应该能够以相同的方式设置评估器:

@evaluation.evaluator = user_that_evaluates

In the view: 在视图中:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM