在我的应用中,评论属于照片,我正在尝试添加一种方法来销毁评论。
的routes.rb
resources :photos do
resources :comments
end
CommentsController
def destroy
@comment = Comment.find(params[:id])
@comment.destroy
end
照片/ show.html.erb
<% @photo.comments.each do |comment| %>
<%= comment.body %></p>
<p><%= link_to 'Remove comment', comment, :confirm => 'Are you sure you want to remove this comment? This cannot be undone.', :method => :delete %></p>
<% end %>
我得到的错误是undefined method 'comment' for #<Photo:0x10ace9270>
。
我想我可能没有正确设置路线,因为当我检查comment
的路线时,我得到了:
rake routes | grep comment
photo_comments GET /photos/:photo_id/comments(.:format) {:action=>"index", :controller=>"comments"}
POST /photos/:photo_id/comments(.:format) {:action=>"create", :controller=>"comments"}
new_photo_comment GET /photos/:photo_id/comments/new(.:format) {:action=>"new", :controller=>"comments"}
edit_photo_comment GET /photos/:photo_id/comments/:id/edit(.:format) {:action=>"edit", :controller=>"comments"}
photo_comment GET /photos/:photo_id/comments/:id(.:format) {:action=>"show", :controller=>"comments"}
PUT /photos/:photo_id/comments/:id(.:format) {:action=>"update", :controller=>"comments"}
DELETE /photos/:photo_id/comments/:id(.:format) {:action=>"destroy", :controller=>"comments"}
有人想过我错在哪里吗?感谢。
答案 0 :(得分:6)
<% @photo.comments.each do |comment| %>
<%= comment.body %></p>
<p><%= link_to 'Remove comment', [@photo, comment], :confirm => 'Are you sure you want to remove this comment? This cannot be undone.', :method => :delete %></p>
<% end %>