Rails - 关联,用户对用户的反馈 - 显示反馈
Rails - associations, feedback on users by users - showing feedback
我正在努力弄清楚如何设置我的 rails 评估模型,以便用户可以使用它给其他用户留下反馈。
我在 post:
中概述了我的问题的关键部分
我从那里收到的建议是按如下方式设置模型:
User.rb
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
Evaluation.rb
belongs_to :evaluator, foreign_key: :evaluator_id, class_name: User
belongs_to :evaluatee, foreign_key: :evaluatee_id, class_name: User
评估控制器
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
@given_evaluations = current_user.given_evaluations
@received_evaluations = current_user.received_evaluations
end
# GET /evaluations/1
# GET /evaluations/1.json
def show
# @received_evaluations = @user.received_evaluations
@evaluation = current_user.received_evaluations.find_by(id: params[:id]) || current_user.given_evaluations.find(params[:id])
# @received_evaluation = current_user.received_evaluations.find params[:id]
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)
@evaluation = current_user.given_evaluations.build(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
current_user.given_evaluations.find(params[:id])
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
current_user.given_evaluations.find(params[:id])
@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?, :evaluatee_id)
end
end
评价表
<%= simple_form_for(@evaluation) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.select :evaluatee_id, User.all.map{|u| [u.formal_name, u.id]} %>
<%= 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 :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 experience", :input_html => {:rows => 10} %>
</div>
<div class="form-actions">
<%= f.button :submit %>
评价秀观点
<% @received_evaluations.each do |receval| %>
<div id="portfolioFiltering" class="masonry-wrapper row">
<%= receval.remark %>
<%#= eval.personal_score %>
<small><%= receval.created_at %></small>
</div>
<% end %>
评估展示视图的替代尝试
<% @given_evaluations.each do |receval| %>
<div id="portfolioFiltering" class="masonry-wrapper row">
<%= receval.remark %>
<%#= eval.personal_score %>
<small><%= receval.created_at %></small>
</div>
<% end %>
我现在遇到的问题是,无论我尝试在节目中展示给定评价还是收到评价,我都会收到一条错误消息:
undefined method `each' for nil:NilClass
我不知道如何设置模型以便用户可以评估另一个用户。我想在他们各自的展示页面上展示每个用户收到的评价。我不知道出了什么问题。我可以从控制台看到用户收到的评价为:
=> #<Evaluation id: 8, evaluatee_id: 34, overall_score: 4, project_score: 5, personal_score: 5, remark: "jhjkhjhjkhkjhjkhjhkhjhkj", work_again?: nil, continue_project?: nil, created_at: "2016-06-12 21:52:53", updated_at: "2016-06-12 21:52:53", evaluator_id: 34>
有一个与我一起工作的用户的条目。但是,我找不到显示该评估的方法。
我能看到的最直接的问题是实例变量:@given_evaluations
和 @received_evaluations
没有在您的 show 动作中设置,并且注释部分正在使用 ActiveRecord#find
方法,这将 return 一个实例,因此您无法循环评估的原因。
我认为显示所有用户评价的更好位置是在 index
操作中,正如您已经在做的那样,您可以将显示的当前逻辑移至索引视图。
要显示每个用户收到的对 show
操作的评价,您可以:
@evaluation = current_user.received_evaluations.find(params[:id])
然后在您的 show
视图中,您应该有类似的内容:
<div id="portfolioFiltering" class="masonry-wrapper row">
<%= @evaluation.remark %>
<%= @evaluation.personal_score %>
<small><%= @evaluation.created_at %></small>
</div>
我正在努力弄清楚如何设置我的 rails 评估模型,以便用户可以使用它给其他用户留下反馈。
我在 post:
中概述了我的问题的关键部分我从那里收到的建议是按如下方式设置模型:
User.rb
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
Evaluation.rb
belongs_to :evaluator, foreign_key: :evaluator_id, class_name: User
belongs_to :evaluatee, foreign_key: :evaluatee_id, class_name: User
评估控制器
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
@given_evaluations = current_user.given_evaluations
@received_evaluations = current_user.received_evaluations
end
# GET /evaluations/1
# GET /evaluations/1.json
def show
# @received_evaluations = @user.received_evaluations
@evaluation = current_user.received_evaluations.find_by(id: params[:id]) || current_user.given_evaluations.find(params[:id])
# @received_evaluation = current_user.received_evaluations.find params[:id]
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)
@evaluation = current_user.given_evaluations.build(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
current_user.given_evaluations.find(params[:id])
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
current_user.given_evaluations.find(params[:id])
@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?, :evaluatee_id)
end
end
评价表
<%= simple_form_for(@evaluation) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.select :evaluatee_id, User.all.map{|u| [u.formal_name, u.id]} %>
<%= 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 :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 experience", :input_html => {:rows => 10} %>
</div>
<div class="form-actions">
<%= f.button :submit %>
评价秀观点
<% @received_evaluations.each do |receval| %>
<div id="portfolioFiltering" class="masonry-wrapper row">
<%= receval.remark %>
<%#= eval.personal_score %>
<small><%= receval.created_at %></small>
</div>
<% end %>
评估展示视图的替代尝试
<% @given_evaluations.each do |receval| %>
<div id="portfolioFiltering" class="masonry-wrapper row">
<%= receval.remark %>
<%#= eval.personal_score %>
<small><%= receval.created_at %></small>
</div>
<% end %>
我现在遇到的问题是,无论我尝试在节目中展示给定评价还是收到评价,我都会收到一条错误消息:
undefined method `each' for nil:NilClass
我不知道如何设置模型以便用户可以评估另一个用户。我想在他们各自的展示页面上展示每个用户收到的评价。我不知道出了什么问题。我可以从控制台看到用户收到的评价为:
=> #<Evaluation id: 8, evaluatee_id: 34, overall_score: 4, project_score: 5, personal_score: 5, remark: "jhjkhjhjkhkjhjkhjhkhjhkj", work_again?: nil, continue_project?: nil, created_at: "2016-06-12 21:52:53", updated_at: "2016-06-12 21:52:53", evaluator_id: 34>
有一个与我一起工作的用户的条目。但是,我找不到显示该评估的方法。
我能看到的最直接的问题是实例变量:@given_evaluations
和 @received_evaluations
没有在您的 show 动作中设置,并且注释部分正在使用 ActiveRecord#find
方法,这将 return 一个实例,因此您无法循环评估的原因。
我认为显示所有用户评价的更好位置是在 index
操作中,正如您已经在做的那样,您可以将显示的当前逻辑移至索引视图。
要显示每个用户收到的对 show
操作的评价,您可以:
@evaluation = current_user.received_evaluations.find(params[:id])
然后在您的 show
视图中,您应该有类似的内容:
<div id="portfolioFiltering" class="masonry-wrapper row">
<%= @evaluation.remark %>
<%= @evaluation.personal_score %>
<small><%= @evaluation.created_at %></small>
</div>