Rails路由返回错误的数据?

时间:2014-04-03 19:41:45

标签: ruby-on-rails ruby-on-rails-3

我有一个rails api,它的模型帖子有很多评论,反之亦然。我使用嵌套路线。

我试图使用路线获取特定帖子的所有评论:

/posts/:post_id/comments 

我发送了一个获取http请求到

http://localhost:3000/posts/1/comments

但无论我在网址中传递了哪个帖子ID,它都会返回数据库中的所有评论。

以下是我的路线的样子,如果您需要查看更多代码,请告诉我。

    post_comments GET    /posts/:post_id/comments(.:format)          comments#index
                  POST   /posts/:post_id/comments(.:format)          comments#create
 new_post_comment GET    /posts/:post_id/comments/new(.:format)      comments#new
edit_post_comment GET    /posts/:post_id/comments/:id/edit(.:format) comments#edit
     post_comment GET    /posts/:post_id/comments/:id(.:format)      comments#show
                  PUT    /posts/:post_id/comments/:id(.:format)      comments#update
                  DELETE /posts/:post_id/comments/:id(.:format)      comments#destroy
            posts GET    /posts(.:format)                            posts#index
                  POST   /posts(.:format)                            posts#create
         new_post GET    /posts/new(.:format)                        posts#new
        edit_post GET    /posts/:id/edit(.:format)                   posts#edit
             post GET    /posts/:id(.:format)                        posts#show
                  PUT    /posts/:id(.:format)                        posts#update
                  DELETE /posts/:id(.:format)                        posts#destroy

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

index的{​​{1}}操作中,请确保在获取CommentsController的记录时指定comments,即获得所有评论特别是post_id

post_id