Rails 4多态关联和关注点
我正在尝试将Evaluation
模型添加到我的Rails 4
应用程序中。
我做了一个名为evaluation.rb
的模型。 它有:
class Evaluation true belongs_to :evaluatable, :polymorphic => true
我也对evaluator
提出了关注并且可以evaluator
为:
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
在我的展示页面中,我想展示特定用户的评估(从其他用户 – 他们是评估者)收到。
在我的节目中,我有:
在我的评估表中,我不确定如何指定评估的接收者。我已经制作了基本表格,但我不清楚如何将其与应该接受评估的用户联系起来。
"How do you rate this experience (1 being did not meet expectations - 10 being met all expectations) ?" %> "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 %>
或者使用集合渲染 。
注意:当前在您视图中的Evaluation.find(...)
应该放在控制器操作中,将它保留在视图中是不好的做法。
如何调整表单以便将用户ID指定为应该接收评估的人?
如果您已识别出可用作evaluatable
的用户,则可以在控制器操作或视图表单中进行设置,以防您在页面上有要评估的用户列表。
在控制器中:
@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 %> <%= 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 %> <% end %> <% end %>
Interesting Posts
修改URL模式Ruby Rails
前置输入标签(simple_form + bootstrap-sass)关闭几个像素
Rails 4,Paperclip,Amazon S3配置Amazon Path
Rails 3:路由错误 – 未初始化的常量MyController :: Google
具有恢复能力的文件上传(最好在Ruby on Rails中)
Active_support / dependencies.rb:247`require’:无法加载此类文件 – 2.1 / pg_ext(LoadError)
Rails 3测试夹具与carrierwave?
Rails 4嵌套资源/路由……差不多……?
Rails 4生成无效的列名称
Ruby没有安装json -v 1.8.1作为bundle的一部分,ERROR:无法构建gem原生扩展