2011-10-29 11 views
1

私のアプリでは、コメントは写真に属しており、コメントを破棄する方法を追加しようとしています。破壊操作を使用したRailsルーティング

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"} 

誰もが私がここで間違っていたところへなどの考えを持っていますか?ありがとう。

答えて

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 %> 
+0

bam!そうです、そのトリックでした。私のルートは大丈夫だと思います。助けてくれてありがとう! –

+0

彼は@photoを渡したからです。コメントは写真の下にネストされているので。 –

関連する問題