Rails 4多态关联和关注点 [英] Rails 4 Polymorphic associations and concerns

查看:62
本文介绍了Rails 4多态关联和关注点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to add an Evaluation model to my Rails 4 app.

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

I have made a model called evaluation.rb. It has:

class Evaluation < ActiveRecord::Base

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

我也对evaluatorevaluatable表示担忧,例如:

I have also made concerns for evaluator and evaluatable as:

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).

在我的节目中,我有:

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

我的评估表具有:

    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

问题

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

How do I setup the show page to show a user's evaluations received?

如何修改表格,以便将用户ID指定为应接收评估的人?

How do I adapt the form so that it specifies a user id as the person who should receive the evaluation?

推荐答案

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

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

Your model concerns should help you with that. In your UsersController#show action, simply adding the following should do the trick:

@received_evaluations = @user.received_evaluations

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

Then you can use it in your show template:

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

或使用集合渲染.

注意:您当前在视图中的Evaluation.find(...)应该放在控制器动作中,将其留在视图中不是一种好习惯.

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.

如何修改表格,以便将用户ID指定为应接收评估的人?

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

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.

在控制器中:

@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

在视图中:

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

这篇关于Rails 4多态关联和关注点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆