我的应用程序成功地每次用户提交一些统计数据时为表创建行。现在我想提供一个删除按钮来删除某些行。
当我点击删除按钮时,我会收到以下错误:
undefined method `destroy' for nil:NilClass
看起来Rails在这种情况下不理解销毁方法,或者由于某些原因Rails没有看到要销毁的内容,因此出现了nil:NilClass。
我的第一个问题是确定哪种情况,如果有的话。
我的第二个问题是如何修复它 :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>
"<%= render @status_updates %>" 调用了 _status_update 部分视图文件。
<tr>
.
.
.
.
<% if current_user==(status_update.user) %>
<td>
<%= link_to "delete", status_update, method: :delete %>
</td>
<% end %>
</tr>
最后,这里是 StatusUpdateController。
def destroy
@status_update.destroy
redirect_to root_url
end
以下是错误页面上的参数:
{"_method"=>"delete",
"authenticity_token"=>"w9eYIUEd2taghvzlo7p3uw0vdOZIVsZ1zYIBxgfBymw=",
"id"=>"106"}
@status_update
?如果没有,那就是问题所在。 - Zach Kemp