在销毁物品上获得错误

时间:2016-06-15 11:58:11

标签: javascript ruby-on-rails ajax

我有1个项目,我试图使用ajax从列表中销毁项目。

class ProjectsController < ApplicationController
.
.
.
 def destroy
    @project.destroy
    respond_to do |format|
      format.js
    end
  end

end

destroy.js

$('#all_projects').html("<%= escape_javascript render(:partial => 'projects/all_projects') %>");

all_projects.html.erb

<table>
  <thead>
    <tr>
      <th>Name</th>
      <th>Team size</th>
      <th colspan="3"></th>
    </tr>
  </thead>

  <tbody>
    <% @projects.each do |project| %>
      <tr>
        <td><%= project.name %></td>
        <td><%= link_to 'Show', project %></td>
        <td><%= link_to 'Edit', edit_project_path(project) %></td>
        <td><%= link_to 'Destroy', project, method: :delete, data: { confirm: 'Are you sure?' } ,:remote=>true%></td>
      </tr>
    <% end %>
  </tbody>
</table>

index.html.erb

<h1>Listing Projects</h1>

<div id="all_project">
  <%= render 'projects/all_projects'%>
</div>

当我尝试加载所有项目时,它工作正常。 但是当我销毁项目时,它会在服务器端给我一个错误,如下所示:

ActionView::Template::Error (undefined method `each' for nil:NilClass):
     9:   </thead>
    10: 
    11:   <tbody>
    12:     <% @projects.each do |project| %>
    13:       <tr>
    14:         <td><%= project.team_size %></td>
  app/views/projects/_all_projects.html.erb:12:in `_app_views_projects__all_projects_html_erb___3489379756657223366_70218666904220'

我错过了什么?

1 个答案:

答案 0 :(得分:0)

此错误是由于缺少对象@project详细信息。

当我将@projects = Project.all放入destroy方法时,此错误消失了,函数运行正常。