导轨 - 对特定用户的反馈,如何设置的形式,以确定相关用户 [英] Rails - feedback on specific users, how to set up the form to identify relevant users

查看:206
本文介绍了导轨 - 对特定用户的反馈,如何设置的形式,以确定相关用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直很努力,在过去的几个月里,弄清楚如何建立一个评价模式,使一些用户可以在他们的联合项目评估的其他用户。

我previously问过这个问题,但因为有一些建议,以使这种模式多态的建议是不正确的方法。我本来就不是能在这个岗位工作得到的意见,但我的问题是,现在略有不同。

<$p$p><$c$c>http://stackoverflow.com/questions/36394489/rails-4-polymorphic-associations-and-concerns

我有一个用户模型和评价模型。

用户

 的has_many:given_evaluations,foreign_key:evaluator_id,取决于:破坏,CLASS_NAME:评估
  的has_many:received_evaluations,foreign_key:evaluatee_id,取决于:破坏,CLASS_NAME:评估

评估

  belongs_to的:评估,foreign_key:evaluator_id,CLASS_NAME:用户
  belongs_to的:被评,foreign_key:evaluatee_id,CLASS_NAME:用户

我现在正试图使正确的用户得到评估,以找出如何设置的形式和限制哪些用户可以离开反馈。

在评价表,我有:

 &LT;%= simple_form_for(@evaluation)做| F | %GT;
  &所述;%= f.error_notification%GT;  &LT; D​​IV CLASS =表单输入&GT;
    &LT;%= f.input:overall_score,收藏:1..10,自动对焦:真的,:标签=&GT; 你如何评价这个项目的经验(1为没有达到预期 - 10得到满足所有人的预期)? %GT;
    &LT;%= f.input:project_score,收藏:1..10,:标签=&GT; 如何成功的项目(1暂时没达到预期 - 10得到满足所有人的预期)? %GT;
    &LT;%= f.input:continue_project ?,为:布尔,checked_value:真,unchecked_value:假:标签=&GT; 你打算继续该项目的工作? %GT;
    &LT;%= f.input:此言一出,如:文字:标签=&GT; 评估你的项目经验:input_html =&GT; {:行数=&GT; 10}%GT;
  &LT; / DIV&GT;  &LT; D​​IV CLASS =表单行动&GT;
    &LT;%= f.button:提交%GT;
  &LT; / DIV&GT;
&LT;%结束%GT;

在我的评价控制器,我有:

 类EvaluationsController&LT; ApplicationController中
  before_action:set_evaluation,只有:[:秀:编辑:更新:灭]
  #的before_filter:GET_USER,只有:[:秀:编辑:更新:灭]  #GET /评估
  #GET /evaluations.json
  高清指数
    @evaluations = Evaluation.all
  结束
  #GET /评估/ 1
  #GET /evaluations/1.json
  高清节目
    @received_evaluations = @ user.received_evaluations
  结束  #GET /评估/新
  高清新
    @evaluation = Evaluation.new
  结束  #GET /评估/ 1 /编辑
  高清编辑
  结束  #POST /评估
  #POST /evaluations.json
  打造高清
    @evaluation = Evaluation.new(evaluation_params)    做的respond_to |格式|
      如果@ evaluation.save
        format.html {redirect_to的@evaluation,通知:评估已成功创建。 }
        format.json {渲染:表演,状态:创建,地点:@evaluation}
      其他
        format.html {渲染:新}
        format.json {渲染JSON:@ evaluation.errors,状态:unprocessable_entity}
      结束
    结束
  结束  #PATCH / PUT /评估/ 1
  #PATCH / PUT /evaluations/1.json
  DEF更新
    做的respond_to |格式|
      如果@ evaluation.update(evaluation_params)
        format.html {redirect_to的@evaluation,通知:评估已成功更新。 }
        format.json {渲染:表演,状态:OK,地点:@evaluation}
      其他
        format.html {渲染:编辑}
        format.json {渲染JSON:@ evaluation.errors,状态:unprocessable_entity}
      结束
    结束
  结束  #删除/评估/ 1
  #删除/evaluations/1.json
  DEF破坏
    @ evaluation.destroy
    做的respond_to |格式|
      format.html {redirect_to的evaluations_url,通知:评估被成功摧毁。 }
      format.json {头:NO_CONTENT}
    结束
  结束  私人的
    #使用回调共享常用的设置或动作之间的约束。
    高清set_evaluation
      @evaluation = Evaluation.find(PARAMS [:ID])
    结束    #切勿轻信参数从可怕的网络,只允许通过白名单。
    高清evaluation_params
      PARAMS [:评测] .permit(:overall_score,:project_score,:personal_score,:此言一出,:work_again?:continue_project?)
    结束
结束

我是否得到了正确的显示控制器动作?我需要把东西在控制器确定哪些用户正在接受评估,并即给予评价。我是否需要添加:USER_ID,以允许PARAMS在评估控制器

我如何改变形式,以确定正在接受评估正确的用户。

我的路线是:

 资源:评估 devise_for:用户,#class_name:FormUser',
             :控制器=&GT; {
                :注册=&GT; 用户/注册,
                #:omniauth_callbacks =&GT; 用户/认证
                :omniauth_callbacks =&GT; 用户/ omniauth_callbacks           }  #得到'/ auth /中:供应商/回调'=&GT; 用户/认证#打造
  #得到'/认证/ SIGN_OUT':以= GT; 用户/认证#销毁  #PER SOURCEY补习----------
  匹配'/用户/:ID / finish_signup'=&GT; 用户#finish_signup',通过:[:搞定了,:补丁]:如= GT; :finish_signup

工作流程


  1. 用户创建一个项目(业主)

  2. 所有者邀请同学加入这个项目(队友)

  3. 如果队友在截止日期接受,参与该项目的用户完成该项目。

  4. 完成后,每个用户计算该项目的其他用户参与和项目本身。要做到这一点,那就是参与该项目的每个用户看到他们的用户页面上的一个按钮,得到一个链接评价表。我需要弄清楚如何引用其他用户ID为队友们以及他们工作的项目。

为什么我没有窝评价路由用户线路内的原因是,我可能会尝试从队友的评价(如果我能想出​​这部分先)单独项目评估,在这种情况下,我想使用评估两个目的。我会回来以后。目前,该评价模型是一个用户。

最后,我用设计的宝石进行用户身份验证。

问题识别被评

考虑保罗的建议如何识别evaluate_id,我添加了这个选择对我的评价形式:

 &LT;%= f.select:evaluatee_id,User.all.map {| U | [u.formal_name]}%GT;

保罗建议,包括这里面行的[] u.id。我不明白,所有的组件如何相互配合在这行code(或意思地图),但我会再次尝试单独找到这些问题的解释。我删除了u.id,因为我得到的错误如下(事实证明,还是删除它会导致下面的错误):

 无法与'ID'= 7找到评价[WHERE评估。evaluatee_id=?]

我可以从该评价保存控制台看到,但它不记录evaluatee_id

  Evaluation.last
  评估负载(10.3ms)选择评估。* FROM评估ORDER BY评估,IDDESC LIMIT 1
 = GT; #&lt;评价ID:7,evaluatee_id:0,overall_score:无,project_score:无,personal_score:无,备注:edededededede,work_again?:无,continue_project?:无,created_at:2016年6月10日02: 08:44的updated_at:2016年6月10日2时08分44秒,evaluator_id:34&GT;

该错误消息指出我的评价控制器的show动作,其中有:

 高清节目
    #@received_evaluations = @ user.received_evaluations    @received_evaluation = current_user.received_evaluations.find PARAMS [:ID]
  结束


解决方案

我同意,多态关联是错误的做法在这里。

您应该在你的#创建方法自动记录与正确评估的评估做到这一点:

  DEF创建
  #更改此设置:@evaluation = Evaluation.new(evaluation_params)
  #这样:
  @evaluation = current_user.given_evaluations.build(evaluation_params)  #...剩下的就是罚款,是...
结束

CURRENT_USER 来自设计,并返回无论用户当前已登录。

我还要确保 #UPDATE 确实 current_user.given_evaluations.find(PARAMS [:ID]) ,这样,你不能改变你的评价没有写。同为 #destroy

根据您的要求,您可能要访问以类似的方式等方法。

#show 方法看起来我错了。它应该只找到的有一个的评价:

 高清节目
  @received_evaluation = current_user.received_evaluations.find PARAMS [:ID]
结束

还有 #INDEX 或许应该被限制在当前的用户。也许你想展示你的都给的加评估你的收到的评价的:

 高清指数
  @given_evaluations = curent_user.given_evaluations
  @received_evaluations = curent_user.received_evaluations
结束

编辑:设置 evaluatee_id ,只是通过它像任何其他参数。第一,允许它来从前端

 高清evaluation_params
  PARAMS [:评测] .permit(
    :overall_score,:project_score,:personal_score,
    :此言一出,:work_again?:continue_project ?,:evaluatee_id)
结束

然后一个小部件添加到您的形式让用户提供。比如,它可能是一个&LT;选择&GT; 列出所有用户:

 &LT;%= f.select:evaluatee_id,User.all.map {| U | [u.id,u.name]}%GT;

这将生成HTML是这样的:

 &LT;选择名称=评价[evaluatee_id]&GT;
  &LT;期权价值=1&GT;乔&LT; /选项&GT;
  &LT;期权价值=2&GT;萨拉&LT; /选项&GT;
  &LT;期权价值=3&GT;弗雷德&LT; /选项&GT;
&LT; /选择&GT;

在上面, 1 2 3 ,你可以选择来评估用户的ID。每个 1所述的内容;选项&GT; 他们的名字。该浏览器将提交选项的值(ID),取其被评你选择。

当然,你应该 u.name 更改到任何列/方法,你实际使用来命名的用户,例如电子邮件 FULL_NAME 或什么的。您可能还需要进行排序和过滤列表---无论是适合您的应用程序。

另外请注意,使用&LT;选择&GT; 只是一个选项。你可以有选择评估前一个页面上被评,并通过形式将它作为一个隐藏字段来代替。或任何你喜欢的!问题的关键是,只要前端发送评测[evaluatee_id] ,你可以保存它像任何其他参数。

I've been trying, for the last few months, to figure out how to set up an evaluation model so that some users can evaluate other users on their joint projects.

I previously asked this question, but have since had some advice that the suggestion to make this model polymorphic was not the right approach. I wasn't actually able to get the advice in this post working, but my question is now slightly different.

http://stackoverflow.com/questions/36394489/rails-4-polymorphic-associations-and-concerns

I have a user model and an evaluation model.

User

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

belongs_to :evaluator, foreign_key: :evaluator_id, class_name: User
  belongs_to :evaluatee, foreign_key: :evaluatee_id, class_name: User

I'm now trying to figure out how to setup the form so that the right user gets the evaluation and to limit the which user can leave the feedback.

In the evaluation form, I have:

<%= 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 %>

In my evaluations controller, I have:

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

Have I got the controller action for show correct? Do I need to put something in the controller to identify which user is receiving the evaluation and which is giving the evaluation. Do I need to add the :user_id to the permitted params in the evaluations controller?

How do I change the form to identify the correct user that's receiving the evaluation.

My routes are:

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

Workflow

  1. A user creates a project (owner)
  2. The owner invites classmates to join the project (team mates)
  3. If team mates accept by the cut off date, the users involved in the project complete the project.
  4. Once complete, each user evaluates the other users involvement in the project and the project itself. To do that, each user that is involved in the project sees a button on their user page, to get a link to the evaluation form. I need to figure out how to reference the other user ids for the team mates as well as the project they worked on.

The reason why I didn't nest the evaluation route inside the user routes is that I might try (if I can figure out this part first) to separate project evaluation from team mate evaluation, in which case, I'd like to use evaluation for two purposes. I'll come back to that later. For now, the evaluation model is on a user.

Finally, I use devise gem for user authentication.

PROBLEMS IDENTIFYING EVALUATEE

Taking Paul's suggestion for how to identify the evaluate_id, I added this select to my evaluation form:

<%= f.select :evaluatee_id, User.all.map{|u| [u.formal_name]} %>

Paul suggested including u.id inside the [] of this line. I don't understand how all the pieces fit together in this line of code (or what map means) but I will try again to find explanations of those issues separately. I removed the u.id, because I was getting the error below (it turns out that removing it still leads to the error below):

Couldn't find Evaluation with 'id'=7 [WHERE "evaluations"."evaluatee_id" = ?]

I can see from the console that the evaluation is saving, but it is not recording the evaluatee_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> 

The error message points to the show action in my evaluations controller, which has:

def show
    # @received_evaluations = @user.received_evaluations

    @received_evaluation = current_user.received_evaluations.find params[:id]
  end

解决方案

I agree that polymorphic associations are the wrong approach here.

You should do this in your #create method to automatically record evaluations with the correct evaluator:

def create
  # change this: @evaluation = Evaluation.new(evaluation_params)
  # to this:
  @evaluation = current_user.given_evaluations.build(evaluation_params)

  # ... the rest is fine as-is ...      
end

The current_user comes from Devise and returns whatever user is currently logged in.

I would also make sure that #update does current_user.given_evaluations.find(params[:id]), so that you can't change evaluations you haven't written. Same for #destroy.

Depending on your requirements you may want to access other methods in a similar way.

Your #show method looks wrong to me. It should only find one evaluation:

def show
  @received_evaluation = current_user.received_evaluations.find params[:id]
end

Also #index should probably be scoped to the current user. Possibly you want to show evaluations you gave plus evaluations you received:

def index
  @given_evaluations = curent_user.given_evaluations
  @received_evaluations = curent_user.received_evaluations
end

EDIT: To set the evaluatee_id, just pass it like any other param. First, permit it to come from the front end:

def evaluation_params
  params[:evaluation].permit(
    :overall_score, :project_score, :personal_score,
    :remark, :work_again?, :continue_project?, :evaluatee_id)
end

Then add a widget to your form to let users provide it. For instance it could be a <select> listing all the users:

<%= f.select :evaluatee_id, User.all.map{|u| [u.id, u.name]} %>

This will generate HTML something like this:

<select name="evaluation[evaluatee_id]">
  <option value="1">Joe</option>
  <option value="2">Sarah</option>
  <option value="3">Fred</option>
</select>

In the above, 1, 2, and 3 are the IDs of the users you can choose to evaluate. The contents of each <option> are their names. The browser will submit the option value (the ID) of whichever evaluatee you choose.

Of course you should change u.name to whatever column/method you actually use to name users, e.g. email or full_name or whatever. Also you might want to sort and filter the list---whatever is appropriate for your app.

Also note that using a <select> is just one option. You could have evaluators choose the evaluatee on a prior page, and pass it through the form as a hidden field instead. Or whatever you like! The point is that as long as the front end is sending an evaluation[evaluatee_id], you can save it like any other param.

这篇关于导轨 - 对特定用户的反馈,如何设置的形式,以确定相关用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆