How to create a delete link for a related object in Ruby on Rails?

后端 未结 3 805
一向
一向 2020-12-24 04:59

So let\'s say I have Posts and Comments and the url for show is /posts/1/comments/1. I want to create a link to delete that comment in the comments controller d

3条回答
  •  情歌与酒
    2020-12-24 05:34

    <%= link_to 'Destroy', post_comment_path(@post, comment),
                data: {:confirm => 'Are you sure?'}, :method => :delete %>
    

    in comments controller:

      def destroy
        @post = Post.find(params[:post_id])
        @comment = Comment.find(params[:id])
        @comment.destroy
    
        respond_to do |format|
          format.html { redirect_to post_comments_path(@post) }
          format.xml  { head :ok }
        end
      end
    

提交回复
热议问题