删除浅嵌套评论 class returns 错误 "Couldn't find Comment without an ID" (Rails)
Deleting shallow nested Comment class returns error "Couldn't find Comment without an ID" (Rails)
我有一个基本的 Rails 应用程序,我在 Post 中浅嵌套了我的评论 class。 Post 嵌套在主题中。我正在尝试使用销毁操作删除评论并提出错误 "Couldn't find Comment without an ID"
错误信息:
ActiveRecord::RecordNotFound at /posts/46/comments
Couldn't find Comment without an ID
CommentsController#destroy
def destroy
**@comment = @post.comments.find(params[:id])**
authorize @post
authorize @comment
if @comment.destroy
flash[:notice] = "Comment was removed."
routes.rb
resources :topics do
resources :posts, except: [:index]
end
resources :posts, only: [] do
resource :comments, only: [:destroy, :create, :new]
end
comments_controller
def destroy
@comment = @post.comments.find(params[:id])
authorize @post
authorize @comment
if @comment.destroy
flash[:notice] = "Comment was removed."
redirect_to :action => 'destroy'
else
flash[:error] = "Comment couldn't be deleted. Try again."
redirect_to [@post.topic, @post]
end
end
注意,我设置了一个before_action来定义@post
@post = Post.find( params[:post_id])
我曾尝试寻找删除 current_session 的方法,而不是寻找评论 ID,但没有成功。
我猜评论中没有附加 id,但根据我的路线我无法理解为什么。当我收集路线时,我得到
post_comments POST /posts/:post_id/comments(.:format) comments#create
new_post_comments GET /posts/:post_id/comments/new(.:format) comments#new
DELETE /posts/:post_id/comments(.:format) comments#destroy
可能问题是删除后没有 /:id /posts/:post_id/comments/:id?
这是我的第一个 rails 应用程序,我对 Ruby 还比较陌生,因此非常感谢任何帮助。再次感谢!
解法:
复数化资源:允许创建 :id 的注释。
Link删除时改成正确的评论id
我们的 Rails 生成评论路由方法将哈希参数作为其参数来生成查询参数。
您假设原因是缺少 :id
参数是正确的。
您拥有的方法将销毁使用以下方法找到的单个评论:@post.comments.find(params[:id])
。
因为:id
没有送进来,方法不知道要销毁哪个评论。
如果您想删除一条评论,则需要相应地修改您的路径,并将 DELETE
更改为 /posts/:post_id/comments/:id
。
如果你想删除一个post的所有评论,你可以将你的方法修改为:
@comments = @post.comments
if @comments.destroy
...
茱莉亚
欢迎来到 Rails 社区!
你完全正确;如果你想删除单个评论,你的路由将需要接受一个 :id 来进行评论。但是,您目前已将其设置为 resource :comments
。你实际上想要使用 resources :comments
所以 Rails 知道你有不止一条评论。
希望对您有所帮助!如果您有任何问题,请告诉我。
我有一个基本的 Rails 应用程序,我在 Post 中浅嵌套了我的评论 class。 Post 嵌套在主题中。我正在尝试使用销毁操作删除评论并提出错误 "Couldn't find Comment without an ID"
错误信息:
ActiveRecord::RecordNotFound at /posts/46/comments
Couldn't find Comment without an ID
CommentsController#destroy
def destroy
**@comment = @post.comments.find(params[:id])**
authorize @post
authorize @comment
if @comment.destroy
flash[:notice] = "Comment was removed."
routes.rb
resources :topics do
resources :posts, except: [:index]
end
resources :posts, only: [] do
resource :comments, only: [:destroy, :create, :new]
end
comments_controller
def destroy
@comment = @post.comments.find(params[:id])
authorize @post
authorize @comment
if @comment.destroy
flash[:notice] = "Comment was removed."
redirect_to :action => 'destroy'
else
flash[:error] = "Comment couldn't be deleted. Try again."
redirect_to [@post.topic, @post]
end
end
注意,我设置了一个before_action来定义@post
@post = Post.find( params[:post_id])
我曾尝试寻找删除 current_session 的方法,而不是寻找评论 ID,但没有成功。
我猜评论中没有附加 id,但根据我的路线我无法理解为什么。当我收集路线时,我得到
post_comments POST /posts/:post_id/comments(.:format) comments#create
new_post_comments GET /posts/:post_id/comments/new(.:format) comments#new
DELETE /posts/:post_id/comments(.:format) comments#destroy
可能问题是删除后没有 /:id /posts/:post_id/comments/:id?
这是我的第一个 rails 应用程序,我对 Ruby 还比较陌生,因此非常感谢任何帮助。再次感谢!
解法:
复数化资源:允许创建 :id 的注释。
Link删除时改成正确的评论id
我们的 Rails 生成评论路由方法将哈希参数作为其参数来生成查询参数。
您假设原因是缺少 :id
参数是正确的。
您拥有的方法将销毁使用以下方法找到的单个评论:@post.comments.find(params[:id])
。
因为:id
没有送进来,方法不知道要销毁哪个评论。
如果您想删除一条评论,则需要相应地修改您的路径,并将 DELETE
更改为 /posts/:post_id/comments/:id
。
如果你想删除一个post的所有评论,你可以将你的方法修改为:
@comments = @post.comments
if @comments.destroy
...
茱莉亚
欢迎来到 Rails 社区!
你完全正确;如果你想删除单个评论,你的路由将需要接受一个 :id 来进行评论。但是,您目前已将其设置为 resource :comments
。你实际上想要使用 resources :comments
所以 Rails 知道你有不止一条评论。
希望对您有所帮助!如果您有任何问题,请告诉我。