Rails 4 多态关联和关注点 [英] Rails 4 Polymorphic associations and concerns
问题描述
我正在尝试将 Evaluation
模型添加到我的 Rails 4
应用程序中.
我制作了一个名为 evaluation.rb
的模型.它有:
类评估 <活动记录::基础属于_to :evaluator, :polymorphic =>真的属于_to :evaluable, :polymorphic =>真的
我还对 evaluator
和 evaluable
表示担忧:
模块评估器扩展 ActiveSupport::Concern包括做has_many :given_evaluations, as: :evaluator, 依赖: :destroy, class_name: 'Evaluation'结尾结尾可评估的模块扩展 ActiveSupport::Concern包括做has_many :received_evaluations, as: :evaluable,dependent: :destroy, class_name: 'Evaluation'结尾结尾
我已将每个问题都包含在我的用户模型中:
类用户 <活动记录::基础包括评估员包括可评估的
在我的展示页面中,我想显示特定用户的评价(从其他用户那里收到 - 他们是评价者).
在我的节目中,我有:
<% 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) 做 |f|%><%= f.error_notification %><div class="form-inputs"><%= f.input :score, collection: 1..10, autofocus: true, :label =>您如何评价这次体验(1 人未达到预期 - 10 人达到所有预期)?"%><%= f.input :remark, as: :text, :label =>"评估你的项目经验", :input_html =>{:rows =>10}%>
我的评估表有:
t.integer "user_id"t.integer "evaluate_id"t.string可评估类型"t.integer "overall_score"t.integer "project_score"t.integer "personal_score"t.text "备注"t.boolean "work_again?"t.boolean "continue_project?"t.datetime "created_at", null: falset.datetime "updated_at", null: false结尾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 做 |evaluation|%>//渲染一些视图的东西<%结束%>
或者使用集合渲染.
注意:当前在您的视图中的 Evaluation.find(...)
应该放在控制器操作中,将其留在视图中不是一个好习惯.
如何调整表单,以便将用户 ID 指定为应接受评估的人员?
如果您已确定将用作 evaluable
的用户,您可以在控制器操作或视图表单中设置它,以防您在页面上有要评估的用户列表.p>
在控制器中:
@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 |user|%><%= simple_form_for(Evaluation.new) 做 |f|%><%= f.error_notification %><div class="form-inputs"><%= f.input :score, collection: 1..10, autofocus: true, :label =>您如何评价这次体验(1 人未达到预期 - 10 人达到所有预期)?"%><%= f.input :remark, as: :text, :label =>"评估你的项目经验", :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 :evaluable_id, :value =>user.id %><%= f.hidden_field :evaluable_type, :value =>user.class.to_s %></div><%结束%><%结束%>
I'm trying to add an Evaluation
model to my Rails 4
app.
I have made a model called 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:
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?
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:
@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.
How do I adapt the form so that it specifies a user id as the person who should receive the evaluation?
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.
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 %>
这篇关于Rails 4 多态关联和关注点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!