轨道尽管在形式上不是属性的改变 [英] rails attribute changes in spite of not being in form

查看:110
本文介绍了轨道尽管在形式上不是属性的改变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Rails应用程序,用户可以将任务分配给对方。每个任务都有一个分配器和一个执行者。默认任务创建者(CURRENT_USER)始终是分配器。无论是出让方和执行者都可以编辑相同的任务。

我的问题如下。比方说,user.id = 2出让方和user.id = 1为遗嘱执行人。如果分配器(ID = 2)编辑任务以后一切正常,但如果遗嘱执行人(ID = 1)编辑它,然后他成为出让方为好,这样task.executor_id = 1和task.assigner_id = 1。有没有选项来改变出让方在新/编辑表单。当分配器创建它自己是默认的分配器作为当前用户,当SBY尝试对其进行编辑,他/她不能改变分配器。是什么原因导致这个问题?

这很奇怪,因为它的工作好早,我有点相信,因为我已经改变使用_task.html.erb部分以&lt我已经遇到此问题;%=渲染@tasks%>,而不是去瓦特/老式@ tasks.each做|任务|版。我首先想到的事情发生,因为我犯了一些错误瓦特/新版本支持Ajax,但在git的复位 - 硬好像是别的东西。

 高清task_params
  params.require(:任务).permit(:executor_id,:姓名,:内容:截止日期:task_name_company)
  .merge(assigner_id:current_user.id)
结束类任务< ActiveRecord的::基地
  belongs_to的:分配器,CLASS_NAME:用户
  belongs_to的:执行者,CLASS_NAME:用户

_task.html.erb

 <若%current_user.id == task.assigner.id%GT;
  < TR风格=背景:#eaeaeaID =task_<%= task.id%GT;>
    < TD类=COL-MD-3>
      <%=的link_to user_path(ID:task.executor.id)办%GT;
        &所述;%= task.executor.profile.first_name%GT; &所述;%= task.executor.profile.last_name%GT; /&下;%= task.executor.profile.company%GT;
      <%结束%GT;
    < / TD>
<%其他%GT;
  &所述; TR的id =task_&下;%= task.id%gt;中与GT;
    < TD类=COL-MD-3>
      <%=的link_to user_path(ID:task.assigner.id)办%GT;
        &所述;%= task.assigner.profile.first_name%GT; &所述;%= task.assigner.profile.last_name%GT; /&下;%= task.assigner.profile.company%GT;
      <%结束%GT;
    < / TD>
<%结束%GT;
    < TD类=冷MD-4>
      <%=的link_to user_task_path(ID:task.id)办%GT;
        &所述;%= task.content%GT;
      <%结束%GT;
    < / TD>
    < TD类=COL-MD-3>
      &所述;%= task.deadline%GT;
    < / TD>
    < TD类=COL-MD-2的风格=填充:0像素>
    <如果%current_user.id == task.assigner.id%GT;
      <表类=表表镶上内表的风格=背景:#eaeaea>
    <%其他%GT;
      <表类=表表镶上内表>
    <%结束%GT;
        &所述; TR>
          < TD类=COL-MD-4的风格=BORDER-顶:0像素;边框底部:0像素,左边框:0像素>
            <如果%task.completed_at ==零%GT;
              <%=的link_to complete_user_task_path(ID:task.id),动作:完成,远程:真,方法:补丁做到%GT;
                < I类=发发检查>< / I>
              <%结束%GT;
            <%其他%GT;
              <%=的link_to uncomplete_user_task_path(ID:task.id),动作:uncomplete,远程:真,方法:补丁做到%GT;
                < I类=发发检查>< / I>
              <%结束%GT;
            <%结束%GT;
          < / TD>
          < TD类=COL-MD-4的风格=BORDER-顶:0像素;边框底部:0像素;>
            <%=的link_to edit_user_task_path(ID:task.id),键入:按钮做%GT;
              < I类=发发铅笔>< I&GT /;
            <%结束%GT;
          < / TD>
          < TD类=COL-MD-4的风格=BORDER-顶:0像素;边框底部:0像素;右边框:0像素>
            <%=的link_to user_task_path(ID:task.id)?你确定:,方法:删除数据:{确认},远程:真正做到%GT;
              < I类=发FA-垃圾>< I&GT /;
            <%结束%GT;
          < / TD>
        < / TR>
      < /表>
    < / TD>
  < / TR>

_form.html.erb

 <%=的form_for([@user,@task])做| F | %GT;  <%=渲染'布局/ error_messages',对象:f.object%GT;  < D​​IV CLASS =字段的形式组>
    &所述;%= f.label:Name_or_Company%GT;
    <%= f.text_field:task_name_company,数据:{autocomplete_source:user_tasknamecompanies_path},班级:的形式控制task_name_company%GT;
  < / DIV>
  < D​​IV CLASS =字段的形式组>
    <%= f.label:含量%GT;
    <%= f.text_area:内容,等级:表单控制%GT;
  < / DIV>
  < D​​IV CLASS =字段的形式组>
    <%= f.label:截止%GT;
    <%= f.date_select:截止日期,等级:表格控%GT;
  < / DIV>
  < D​​IV CLASS =行动>
    <%= f.submit创建任务类:BTN BTN-小学,数据SID=> current_user.id,数据哧=> :executor_id%>
  < / DIV>
<%结束%GT;

更新:
基于丰富的佩克和miler350的答案,问题就迎刃而解了。我做了以下内容:

  before_action:set_assigner,只有:创建私人的高清set_assigner
  @ task.assigner.id = current_user.id
结束


解决方案

 高清task_params
  params.require(:任务).permit(:executor_id,:姓名,:内容:截止日期:task_name_company).merge(assigner_id:current_user.id)
结束

您正在设置分配器ID来CURRENT_USER每次在PARAMS打发时间。

我会删除合并,只是有较强的PARAMS是这样的:

  params.require(:任务).permit(:executor_id,:姓名,:内容:截止,:task_name_company,:assigner_id)

然后,在你创建行动,在此之前:

  @ task.save

补充一点:

  @ task.assigner_id = current_user.id

然后,你可以定义你的权限无论您选择(必须是分配器,必须执行者),但只要你不添加任何疯狂的更新,它会正常工作。

I have rails app where users can assign tasks to each other. Every task has one assigner and one executor. By default the task creator(current_user) is always the assigner. Both of the assigner and executor are allowed to edit the same task.

My problem is the following. Let's say user.id=2 the assigner and user.id=1 is the executor. If the assigner (id=2) edits the task later on everything works fine, but if the executor (id=1) edits it then he becomes the assigner as well, so task.executor_id = 1 and task.assigner_id = 1. There is no option to change the assigner in the new/edit form. When assigner creates it he is the default assigner as current user and when sby tries to edit it he/she can't change the assigner. What causes this issue?

It's weird since it worked well earlier and I'm kinda sure that I have been experiencing this problem since I've changed to using the _task.html.erb partial with <%= render @tasks %> instead of going w/ the plain old @tasks.each do |task| version. I first thought it happened because I made some mistakes w/ the new AJAXified version, but after git reset --hard it seems to be something else.

def task_params
  params.require(:task).permit(:executor_id, :name, :content, :deadline, :task_name_company)
  .merge(assigner_id: current_user.id)
end

class Task < ActiveRecord::Base
  belongs_to :assigner, class_name: "User"
  belongs_to :executor, class_name: "User"

_task.html.erb

<% if current_user.id == task.assigner.id %>
  <tr style="background: #eaeaea" id="task_<%= task.id %>">
    <td class="col-md-3">
      <%= link_to user_path(id: task.executor.id) do %>
        <%= task.executor.profile.first_name %> <%= task.executor.profile.last_name%> / <%= task.executor.profile.company %>
      <% end %>
    </td>
<% else %>
  <tr id="task_<%= task.id %>">
    <td class = "col-md-3">
      <%= link_to user_path(id: task.assigner.id) do %>
        <%= task.assigner.profile.first_name %> <%= task.assigner.profile.last_name%> / <%= task.assigner.profile.company %>
      <% end %>
    </td>
<% end %>
    <td class="cold-md-4">
      <%= link_to user_task_path(id: task.id) do %>
        <%= task.content %>
      <% end %>
    </td>
    <td class="col-md-3">
      <%= task.deadline %>
    </td>
    <td class="col-md-2" style="padding:0px">
    <% if current_user.id == task.assigner.id %>
      <table class="table table-bordered inside-table" style="background:#eaeaea">
    <% else %>
      <table class="table table-bordered inside-table">
    <% end %>
        <tr>
          <td class="col-md-4" style="border-top:0px;border-bottom:0px;border-left:0px">
            <% if task.completed_at == nil %>
              <%= link_to complete_user_task_path(id: task.id), action: :complete, remote: true, method: :patch do %>
                <i class="fa fa-check"></i>
              <% end %>
            <% else %>
              <%= link_to uncomplete_user_task_path(id: task.id), action: :uncomplete, remote: true, method: :patch do %>
                <i class="fa fa-check"></i>
              <% end %>
            <% end %>
          </td>
          <td class="col-md-4" style="border-top:0px;border-bottom:0px;">
            <%= link_to edit_user_task_path(id: task.id), type: "button" do %>
              <i class="fa fa-pencil"></i>
            <% end %>
          </td>
          <td class="col-md-4" style="border-top:0px;border-bottom:0px;border-right:0px">
            <%= link_to user_task_path(id: task.id), method: :delete, data: { confirm: "Are you sure?" }, remote: true do %>
              <i class="fa fa-trash"></i>
            <% end %>
          </td>
        </tr>
      </table>
    </td>
  </tr>

_form.html.erb

<%= form_for ([@user, @task]) do |f| %>

  <%= render 'layouts/error_messages', object: f.object %>

  <div class="field form-group">
    <%= f.label :Name_or_Company %>
    <%= f.text_field :task_name_company, data: {autocomplete_source: user_tasknamecompanies_path}, class: "form-control task_name_company" %>
  </div>
  <div class="field form-group">
    <%= f.label :content %>
    <%= f.text_area :content, class: "form-control" %>
  </div>
  <div class="field form-group">
    <%= f.label :deadline %>
    <%= f.date_select :deadline, class: "form-control" %>
  </div>
  <div class="actions">
    <%= f.submit "Create Task", class: 'btn btn-primary', "data-sid" => current_user.id, "data-rip" => :executor_id %>
  </div>
<% end %>

UPDATE: Problem solved based on Rich Peck's and miler350's answers. I did the following:

before_action :set_assigner, only: :create

private

def set_assigner
  @task.assigner.id = current_user.id
end

解决方案

def task_params
  params.require(:task).permit(:executor_id, :name, :content, :deadline, :task_name_company).merge(assigner_id: current_user.id)
end

You are setting the assigner id to current_user every time you pass in the params.

I would remove the merge, and just have strong params like this:

params.require(:task).permit(:executor_id, :name, :content, :deadline, :task_name_company, :assigner_id)

Then, in your create action, before you do:

@task.save

Add this:

@task.assigner_id = current_user.id

Then you can define your permissions however you choose (must be assigner, must be executor), but as long as you don't add anything crazy to update, it'll work fine.

这篇关于轨道尽管在形式上不是属性的改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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