Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails rails中的删除操作之后返回什么类_Ruby On Rails_Rails Ujs - Fatal编程技术网

Ruby on rails rails中的删除操作之后返回什么类

Ruby on rails rails中的删除操作之后返回什么类,ruby-on-rails,rails-ujs,Ruby On Rails,Rails Ujs,我正在使用rails ujs删除一个对象, 我的目标是在我删除它之后隐藏那个div和obj,我以前做过,但这次我不能得到它,我想我需要知道发生了什么。 查看: <div class="employee_<%= employee.id %>" <%= link_to 'Destroy', employee, method: :delete, remote: true } %> </div 控制台错误: NoMethodError in Employees#d

我正在使用rails ujs删除一个对象, 我的目标是在我删除它之后隐藏那个div和obj,我以前做过,但这次我不能得到它,我想我需要知道发生了什么。 查看:

<div class="employee_<%= employee.id %>" 
<%= link_to 'Destroy', employee, method: :delete, remote: true } %>
</div
控制台错误:

NoMethodError in Employees#destroy

Showing 
/Desktop/bad/current/app/views/employees/destroy.js.erb where line #1 
raised:
undefined method `id' for nil:NilClass

Rails.root: /Desktop/bad/current

Application Trace
app/views/employees/destroy.js.erb:1:in 
`_app_views_employees_destroy_js_erb__4462192751889852231_70177039108760'

一旦你销毁了一条记录,你就不应该再使用它,它也不再有
id
。对于您的情况,您可以在控制器中删除之前保存id:

format js
@deleted\u employee\u id=@employee.id
@雇员:销毁
...
在js视图中,
…查找('div.employee').hide()…


另外,使用“软删除”也是一种常见做法——在模型中有一个标志,指示记录是否被“删除”,并在
destroy
操作中进行设置,如果用户希望“撤消”,则在没有实际删除的情况下,这是很有用的。

一旦您销毁了一条记录,您就不应该再使用它,它也不再有
id
。对于您的情况,您可以在控制器中删除之前保存id:

format js
@deleted\u employee\u id=@employee.id
@雇员:销毁
...
在js视图中,
…查找('div.employee').hide()…


另外,使用“软删除”也是一种常见的做法——在模型中有一个标志,指示记录是否被“删除”,并将其设置在
destroy
操作中,如果用户想要“撤销”,则在没有实际删除的情况下,这很有用。

谢谢,我没有花2小时试图弄清楚。谢谢,我没有花2小时试图弄清楚。