【发布时间】: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