【发布时间】:2023-03-26 23:53:01
【问题描述】:
在过去的几个月里,我一直在尝试弄清楚如何建立一个评估模型,以便一些用户可以在他们的联合项目中评估其他用户。
我之前问过这个问题,但后来有人建议让这个模型多态的建议不是正确的方法。我实际上无法获得这篇文章中的建议,但我的问题现在略有不同。
http://stackoverflow.com/questions/36394489/rails-4-polymorphic-associations-and-concerns
我有一个用户模型和一个评估模型。
用户
has_many :given_evaluations, foreign_key: :evaluator_id, dependent: :destroy, class_name: Evaluation
has_many :received_evaluations, foreign_key: :evaluatee_id, dependent: :destroy, class_name: Evaluation
评估
belongs_to :evaluator, foreign_key: :evaluator_id, class_name: User
belongs_to :evaluatee, foreign_key: :evaluatee_id, class_name: User
我现在正试图弄清楚如何设置表单,以便正确的用户获得评估并限制哪些用户可以留下反馈。
在评估表中,我有:
<%= simple_form_for(@evaluation) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :overall_score, collection: 1..10, autofocus: true, :label => "How do you rate this project experience (1 being did not meet expectations - 10 being met all expectations) ?" %>
<%= f.input :project_score, collection: 1..10, :label => "How successful was the project (1 being did not meet expectations - 10 being met all expectations)?" %>
<%= f.input :continue_project?, as: :boolean, checked_value: true, unchecked_value: false, :label => "Do you intend to continue working on the project?" %>
<%= f.input :remark, as: :text, :label => "Evaluate your project experience", :input_html => {:rows => 10} %>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
在我的评估控制器中,我有:
class EvaluationsController < ApplicationController
before_action :set_evaluation, only: [:show, :edit, :update, :destroy]
# before_filter :get_user, only: [:show, :edit, :update, :destroy]
# GET /evaluations
# GET /evaluations.json
def index
@evaluations = Evaluation.all
end
# GET /evaluations/1
# GET /evaluations/1.json
def show
@received_evaluations = @user.received_evaluations
end
# GET /evaluations/new
def new
@evaluation = Evaluation.new
end
# GET /evaluations/1/edit
def edit
end
# POST /evaluations
# POST /evaluations.json
def create
@evaluation = Evaluation.new(evaluation_params)
respond_to do |format|
if @evaluation.save
format.html { redirect_to @evaluation, notice: 'Evaluation was successfully created.' }
format.json { render :show, status: :created, location: @evaluation }
else
format.html { render :new }
format.json { render json: @evaluation.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /evaluations/1
# PATCH/PUT /evaluations/1.json
def update
respond_to do |format|
if @evaluation.update(evaluation_params)
format.html { redirect_to @evaluation, notice: 'Evaluation was successfully updated.' }
format.json { render :show, status: :ok, location: @evaluation }
else
format.html { render :edit }
format.json { render json: @evaluation.errors, status: :unprocessable_entity }
end
end
end
# DELETE /evaluations/1
# DELETE /evaluations/1.json
def destroy
@evaluation.destroy
respond_to do |format|
format.html { redirect_to evaluations_url, notice: 'Evaluation was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_evaluation
@evaluation = Evaluation.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def evaluation_params
params[:evaluation].permit(:overall_score, :project_score, :personal_score, :remark, :work_again?, :continue_project?)
end
end
我的控制器操作是否正确?我是否需要在控制器中放置一些东西来识别哪个用户正在接受评估以及哪个用户正在给出评估。我是否需要在评估控制器中将 :user_id 添加到允许的参数中?
如何更改表单以识别正在接受评估的正确用户。
我的路线是:
resources :evaluations
devise_for :users, #class_name: 'FormUser',
:controllers => {
:registrations => "users/registrations",
# :omniauth_callbacks => "users/authentications"
:omniauth_callbacks => 'users/omniauth_callbacks'
}
# get '/auth/:provider/callback' => 'users/authentications#create'
# get '/authentications/sign_out', :to => 'users/authentications#destroy'
# PER SOURCEY TUTORIAL ----------
match '/users/:id/finish_signup' => 'users#finish_signup', via: [:get, :patch], :as => :finish_signup
工作流程
- 用户创建项目(所有者)
- 业主邀请同学加入项目(队友)
- 如果队友在截止日期前接受,则参与项目的用户完成项目。
- 一旦完成,每个用户都会评估参与项目的其他用户和项目本身。为此,项目中涉及的每个用户都会在其用户页面上看到一个按钮,以获取指向评估表的链接。我需要弄清楚如何为团队成员以及他们从事的项目引用其他用户 ID。
我没有将评估路线嵌套在用户路线中的原因是我可能会尝试(如果我能先弄清楚这部分)将项目评估与队友评估分开,在这种情况下,我想将评估用于两个目的。我稍后再谈。目前,评估模型是针对用户的。
最后,我使用 devise gem 进行用户身份验证。
识别被评估者的问题
采纳 Paul 关于如何识别评估 ID 的建议,我将此选择添加到我的评估表中:
<%= f.select :evaluatee_id, User.all.map{|u| [u.formal_name]} %>
Paul 建议在这一行的 [] 中包含 u.id。我不明白所有部分如何在这行代码中组合在一起(或 map 的含义),但我会再次尝试分别找到这些问题的解释。我删除了 u.id,因为我收到了下面的错误(事实证明,删除它仍然会导致下面的错误):
Couldn't find Evaluation with 'id'=7 [WHERE "evaluations"."evaluatee_id" = ?]
我可以从控制台看到评估正在保存,但它没有记录评估者ID。
Evaluation.last
Evaluation Load (10.3ms) SELECT "evaluations".* FROM "evaluations" ORDER BY "evaluations"."id" DESC LIMIT 1
=> #<Evaluation id: 7, evaluatee_id: 0, overall_score: nil, project_score: nil, personal_score: nil, remark: "edededededede", work_again?: nil, continue_project?: nil, created_at: "2016-06-10 02:08:44", updated_at: "2016-06-10 02:08:44", evaluator_id: 34>
错误消息指向我的评估控制器中的显示操作,它具有:
def show
# @received_evaluations = @user.received_evaluations
@received_evaluation = current_user.received_evaluations.find params[:id]
end
【问题讨论】:
-
还请提供您的路线 (routes.rb) 以了解评估表单的范围 - 它是用户的嵌套资源还是独立的? URL 是控制器 /evaluations/1 中的注释还是 /users/:id/evaluations/1?
-
同样,描述创建评估的逻辑(工作流程)。我是否打开所有用户的列表,然后打开单个用户的所有项目,然后创建评估?或者我有所有项目的清单吗?我究竟在哪里按下“评估”按钮来创建新的评估?到目前为止,在 new 和 create 控制器操作上,您都创建了新的未绑定评估,既与当前用户(给出评估的人)无关,也与评估的用户(您给出评估的人)无关
标签: ruby-on-rails associations