【问题标题】:How to delete a comment that is nested inside of a post, which is nested inside of a topic?如何删除嵌套在帖子内的评论,该评论嵌套在主题内?
【发布时间】:2014-12-05 03:19:42
【问题描述】:

我正在尝试删除嵌套在帖子中的评论。所述帖子嵌套在主题内。我对 RoR 还很陌生,在提出问题之前尝试自己做一些事情,在这里真的需要帮助。我当前的设置删除了整个帖子而不是 评论

routes.rb

Rails.application.routes.draw do

  devise_for :users

  resources :users, only: [:update]

  resources :topics do
    resources :posts, except: [:index]
  end

  resources :posts, only: [] do
    resources :comments, only: [:create, :destroy]
  end
end

(模型)topic.rb

class Topic < ActiveRecord::Base
 has_many :posts, dependent: :destroy
end

(模型)post.rb

class Post < ActiveRecord::Base
 has_many :comments, dependent: :destroy
 belongs_to :user
 belongs_to :topic
end

(模型)comment.rb

class Comment < ActiveRecord::Base
 belongs_to :post
 belongs_to :user
end

comment_controller

def destroy
  @topic = Topic.find(params[:topic_id])
  @post = Post.find(params[:post_id])
  @comment = @post.comments.find(params[:id])

 authorize @comment
  if @comment.destroy
   flash[:notice] = "Comment was removed"
   redirect_to [@post.post, @post]
  else
   flash[:notice] = "There was an error removing comment"
   redirect_to [@topic, @post]
  end
end 

部分正在视图/帖子/节目中呈现

link_to "Delete", [@topic, @post], method: :delete, data: {confirm: "Are you sure you want to delete this ?"}

【问题讨论】:

    标签: ruby-on-rails nested


    【解决方案1】:

    你应该改变这个

    link_to "Delete", [@topic, @post], method: :delete, data: {confirm: "Are you sure you want to delete this ?"}
    

    到这里

    link_to "Delete", [@post, @comment], method: :delete, data: {confirm: "Are you sure you want to delete this ?"}
    

    因此,该链接为您提供了一条带有 DELETE http 方法的类似 posts/:post_id/cmets/:id 的路径。

    以及 cmets 的控制器:

    def destroy
      @topic = Topic.find(params[:topic_id])
      @post = Post.find(params[:post_id])
      @comment = @post.comments.find(params[:id])
    
      authorize @comment
      if @comment.destroy
        flash[:notice] = "Comment was removed"
        redirect_to @post
      else
        flash[:notice] = "There was an error removing comment"
        redirect_to [@topic, @post]
      end
    end 
    

    我刚刚更改了第一个redirect_to,因为我不知道你为什么要使用redirect_to [@post.post, @post]

    【讨论】:

      【解决方案2】:

      在视图/帖子/节目上呈现的部分我希望删除整个帖子。

      如果有 cmets 视图,您可能会显示类似这样的内容,并将路线更新为您的路线,我猜了一下:

      link_to 'Delete', post_comment_path(@post, @comment), method: :delete, data: { confirm: 'Are you sure?' }
      

      或者,如果您有 cmets 的索引视图,这可能会起作用:

      link_to 'Delete', comment, method: :delete, data: { confirm: 'Are you sure?' }
      

      此外,Rails 指南的这一部分可能会有所帮助:http://guides.rubyonrails.org/routing.html#nested-resources

      祝你好运!

      【讨论】:

      • 你是说我需要观点/评论/节目吗?我目前根本没有。说明只允许销毁路线 :destroy 和 :create
      • 您可能不需要设置视图,但无论如何我认为您需要不同的删除路线来发表评论,类似于post_comment_path(@post, @comment)。如果要删除整个帖子,则评论上的删除按钮将链接到删除整个帖子。
      • 我在评论中尝试了您的上述解决方案,然后收到“无法找到没有 ID 错误的主题”它在 URL 中显示帖子 ID 和评论 ID,但没有主题 ID,这是什么东西与我在 cmets 的销毁方法下的重定向有关?
      • 也许...您也可以尝试 post_comment_path(@topic, @post, @comment) ,具体取决于跟踪显示的内容。您还可以使用 gem better errors github.com/charliesome/better_errors 获得更易读的错误,并使用终端窗口尝试不同的选项来获得不同的错误/看看什么有效。
      猜你喜欢
      • 2021-09-09
      • 1970-01-01
      • 2016-01-31
      • 2012-09-17
      • 1970-01-01
      • 1970-01-01
      • 2014-08-28
      • 1970-01-01
      • 2015-09-01
      相关资源
      最近更新 更多