如何在 Ruby on Rails 中为相关对象创建删除链接? [英] How to create a delete link for a related object in Ruby on Rails?

查看:19
本文介绍了如何在 Ruby on Rails 中为相关对象创建删除链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有帖子和评论,显示的 url 是 /posts/1/comments/1.我想创建一个链接以在评论控制器销毁方法中删除该评论.我该怎么做?

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 destroy method. How do I do that?

推荐答案

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

在评论控制器中:

  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

这篇关于如何在 Ruby on Rails 中为相关对象创建删除链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆