Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/63.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 未定义的方法`销毁';零级:零级_Ruby On Rails_Ruby On Rails 3_Ruby On Rails 3.1_Ruby On Rails 3.2 - Fatal编程技术网

Ruby on rails 未定义的方法`销毁';零级:零级

Ruby on rails 未定义的方法`销毁';零级:零级,ruby-on-rails,ruby-on-rails-3,ruby-on-rails-3.1,ruby-on-rails-3.2,Ruby On Rails,Ruby On Rails 3,Ruby On Rails 3.1,Ruby On Rails 3.2,每次用户提交一些统计数据时,我的应用程序都会成功地为表创建行。现在我想提供一个delete按钮来删除一些行 单击“删除”按钮时,会出现以下错误: undefined method `destroy' for nil:NilClass 看起来要么rails不理解本例中的destroy方法,要么rails看不到任何要销毁的内容,因此出现了nil:nil类 我的第一个问题是确定哪种情况是这样的,如果这两种情况都是这样的话 我的第二个问题是修正它:D 这是我的show.html: <% prov

每次用户提交一些统计数据时,我的应用程序都会成功地为表创建行。现在我想提供一个delete按钮来删除一些行

单击“删除”按钮时,会出现以下错误:

undefined method `destroy' for nil:NilClass
看起来要么rails不理解本例中的destroy方法,要么rails看不到任何要销毁的内容,因此出现了nil:nil类

我的第一个问题是确定哪种情况是这样的,如果这两种情况都是这样的话

我的第二个问题是修正它:D

这是我的show.html:

<% provide(:title, "Log" ) %>
<% provide(:heading, "Your Progress Log") %>

<div class="row">  
  <div class="span8">
    <% if @user.status_update.any? %>
      <h3>Status Updates (<%= @user.status_update.count %>)</h3>    
      <table>
        <thead>
          <tr>
            <th>Entry Date</th>
            <th>Weight</th>
            <th>BF %</th>
            <th>LBM</th>
            <th>Fat</th>
            <th>Weight Change</th>
            <th>BF % Change</th>
            <th>Fat Change</th>
          </tr>
        </thead>
      <tbody class = "status updates">
          <%= render @status_updates %>
      </tbody>        
    <% end %>
  </div>
</div>
以下是错误页面上的参数:

{"_method"=>"delete",
 "authenticity_token"=>"w9eYIUEd2taghvzlo7p3uw0vdOZIVsZ1zYIBxgfBymw=",
 "id"=>"106"}

由于您没有显示任何可以分配
@status\u update
的代码(过滤器等),因此我假设没有。因此,
destroy
方法中的实例变量
@status\u update
。您需要查询具有对象ID的类

按如下方式重写销毁方法:

def destroy
    @status_update = StatusUpdate.find(params[:id])
    if @status_update.present?
      @status_update.destroy
    end
    redirect_to root_url
end

注意:用实际的类名替换类名
StatusUpdate

@status\u update未在控制器中定义。您需要通过参数(params[:id])访问它,或者如果使用资源,则访问resource.destroy。检查您的rake路由以确定要使用的参数。您是否在销毁方法的
过滤器之前的
@status\u update
中指定了
@status\u update
?如果不是,那就是你的问题所在。我明白了。我正在为一个不同的项目学习一个教程,有些代码我不理解,但我现在看到的正是这些代码。它有一个名为“correct user”的私有函数,将@status\u update实例分配给一个对象id。感谢您的帮助。如何调用该方法以及何时调用该方法?在before筛选器中。我把:放在\u过滤器之前:correct\u user,only::destroy,然后在private下我有:private def correct\u user@status\u update=current\u user.status\u update.find\u by_id(params[:id])如果@status\u update.nil?endTry将
:destroy
放入一个数组中,如
在\u filter:correct\u user,:only=>[:destroy]
之前。
{"_method"=>"delete",
 "authenticity_token"=>"w9eYIUEd2taghvzlo7p3uw0vdOZIVsZ1zYIBxgfBymw=",
 "id"=>"106"}
def destroy
    @status_update = StatusUpdate.find(params[:id])
    if @status_update.present?
      @status_update.destroy
    end
    redirect_to root_url
end