我的应用程序每次用户提交一些统计数据时都会成功为表创建行。现在,我想提供一个删除按钮以删除一些行。
当我单击删除按钮时,我会收到错误:
undefined method `destroy' for nil:NilClass
似乎任何原因都不理解销毁方法,无论出于何种原因,或者铁轨都没有看到任何销毁的东西,因此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>
"&lt;%=渲染@status_updates%>"调用_status_update partial。
<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
的代码(滤波器左右),因此我认为没有任何代码。因此,未设置您在destroy
方法中的实例变量@status_update
。您需要使用对象的ID查询类。
重写您的销毁方法如下:
def destroy
@status_update = StatusUpdate.find(params[:id])
if @status_update.present?
@status_update.destroy
end
redirect_to root_url
end
注意:将ClassName StatusUpdate
替换为您的实际类名称。