我的女孩模型使用的宝石“acts_as_commentable的评论如何删除嵌套记录
当我访问example.com/girls/show/1
它显示ID#1女孩的个人资料。 所有发布的评论都显示在此页面的底部。
对于每个注释行,我要添加删除按钮来删除评论。
如果它应该通过参数girls_controller.rb的comment_destroy行动。 动作部分和视图应该如何?
它保持未定义的局部变量或方法`女孩'错误代码如下。
“女孩/ show.html.erb”的观点应该是这样的。只是一部分。
<table>
<tr>
<th>ID</th>
<th>Title</th>
<th>Body</th>
<th>Subject</th>
<th>Delete</th>
</tr>
<% @all_comments.each do |comment| %>
<tr>
<td><%= comment.id %></td>
<td><%= comment.title %></td>
<td><%= comment.body %></td>
<td><%= comment.subject %></td>
<td><%= button_to 'comment_destroy', girls, confirm: 'Are you sure?', :disable_with => 'deleting...', method: :delete %></td>
</tr>
<% end %>
</table>
girls_controller.rb的comment_destroy行动应该是这样的
def comment_destroy
@comment = comment.find(params[:id])
@comment.destroy
respond_to do |format|
format.html { redirect_to girls_url }
format.json { head :ok }
end
redirect_to :controller => 'girls', :action => 'show', :id => params[:girls][:id]
flash[:notice] = "comment deleted!"
end
您应该将路由添加到您的问题。但似乎你需要阅读和研究如何处理CRUD。看看官方指南,railscasts或代码学习网站上的Rails for zombies课程 – 2012-07-08 08:10:57
使用AJAX或查看'_destroy'“参数”。 – Zabba 2012-07-08 08:47:50
谢谢!我应该添加什么来查看部分来传递参数? – MKK 2012-07-08 10:16:56