我在db / config
中有以下资源resources :posts do
resources :comments
end
我的控制器内部有以下操作,名为Comment
def destroy
@post = Post.find(params[:post_id])
@comment = @post.comments.find(params[:id])
@comment.destroy
redirect_to post_path(@post)
end
当我删除评论栏重定向到@post
的网址时example /posts/1
但我不想重定向到该网址,而是
posts/1/comments
答案 0 :(得分:3)
使用
redirect_to post_comments_path(@post)
在命令行中,如果您执行rake routes
,它会显示:
Prefix Verb URI Pattern Controller#Action
post_comments GET /posts/:post_id/comments(.:format) comments#index
post_comments
前缀可让您知道有两个可用的帮助:post_comments_path
和post_comments_url
,用于生成关联的路径或网址。