【发布时间】:2016-05-29 06:08:38
【问题描述】:
我正在尝试弄清楚如何在我的 Rails 4 应用中实现评估模型。
我之前已经问过这些相关的问题,但还没有解决这个问题:
Rails 4 Polymorphic associations and concerns
Rails 4 - post completion evaluations model - structure
我有:
class Evaluation < ActiveRecord::Base
belongs_to :evaluator, :polymorphic => true
belongs_to :evaluatable, :polymorphic => true
我还对 evaluator 和 evaluable 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
我已将每个问题都包含在我的用户模型中:
class User < ActiveRecord::Base
include Evaluator
include Evaluatable
在我的显示页面中,我想显示特定用户的评价(从其他用户收到 - 他们是评价者)。
在我的节目中,我有:
<% Evaluation.find(params[:id]).received_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
在我的用户控制器中,我有:
def show
# authorize! :read, @user
@received_evaluations = @user.received_evaluations
end
我目前收到一条错误消息:
undefined method `received_evaluations' for #<Evaluation:0x007fb8c4b32160>
我不确定这条消息的含义或如何解决它。
如果我将节目更改为:
<% @user.received_evaluations.each do |eval| %>
我收到此错误:
undefined method `received_evaluations' for nil:NilClass
【问题讨论】:
标签: ruby-on-rails ruby polymorphic-associations