我正在尝试将评估模型添加到我的Rails 4应用程序中.
我做了一个名为evaluation.rb的模型.它有:
class Evaluation < ActiveRecord::Base belongs_to :evaluator,:polymorphic => true belongs_to :evaluatable,:polymorphic => true
我也对评估者提出了关注并且可以评估为:
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,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} %>
我的评估表有:
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 action中,只需添加以下内容即可:
@received_evaluations = @user.received_evaluations
然后你可以在你的节目模板中使用它:
<% @received_evaluations.each do |evaluation| %> // render some view stuff <% end %>
或者使用collection rendering.
注意:当前在您视图中的Evaluation.find(…)应该放在控制器操作中,将它保留在视图中是不好的做法.
如何调整表单以便将用户ID指定为应该接收评估的人?
如果您已识别出可用作评估的用户,则可以在控制器操作或视图表单中进行设置,以防您在页面上有要评估的用户列表.
在控制器中:
@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,:label => "How do you rate this experience (1 being did not meet expectations - 10 being met all expectations) ?" %> <%= f.input :remark,: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 %>