Ruby on rails 4,设置routes.rb和资源

时间:2016-06-23 14:29:37

标签: ruby-on-rails-4 routes

我不知道如何在资源方法中设置多于1个“项目”。我有3个支架,即邮政,评论,回应。我想在rake路线中达到这个目标:'new_post_comment_response => /帖/ ID /评论/ ID /响应/新'

关系是:

'发布模型 有很多:评论 有很多:回应

评论模型 有很多:回应 belongs_to:post

响应模型 belongs_to:post belongs_to:comment'

在routes.rb中我设置了:

资源:帖子做 资源:评论,除了:[:show,:index] 端

资源:评论 资源:响应,除了:[:show,:index] 端

但是我想在rake路线中有三个项目,因为我要做一些“论坛中的三个回复(评论后回复评论)”。你知道我的意思吗?

所以,我的问题是:如何设置资源和控制器来检测帖子和评论ID,以及如何设置资源。

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

您基本上只需要按照您希望的方式嵌套资源。这意味着帖子应该有嵌套的评论和响应,评论应该有嵌套的响应。假设我正确地阅读了您的要求,您可以根据这一点设置一些内容。

    post_comment_responses POST   /posts/:post_id/comments/:comment_id/responses(.:format)          responses#create
 new_post_comment_response GET    /posts/:post_id/comments/:comment_id/responses/new(.:format)      responses#new
edit_post_comment_response GET    /posts/:post_id/comments/:comment_id/responses/:id/edit(.:format) responses#edit
     post_comment_response PATCH  /posts/:post_id/comments/:comment_id/responses/:id(.:format)      responses#update
                           PUT    /posts/:post_id/comments/:comment_id/responses/:id(.:format)      responses#update
                           DELETE /posts/:post_id/comments/:comment_id/responses/:id(.:format)      responses#destroy
             post_comments 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 PATCH  /posts/:post_id/comments/:id(.:format)                            comments#update
                           PUT    /posts/:post_id/comments/:id(.:format)                            comments#update
                           DELETE /posts/:post_id/comments/:id(.:format)                            comments#destroy
            post_responses POST   /posts/:post_id/responses(.:format)                               responses#create
         new_post_response GET    /posts/:post_id/responses/new(.:format)                           responses#new
        edit_post_response GET    /posts/:post_id/responses/:id/edit(.:format)                      responses#edit
             post_response PATCH  /posts/:post_id/responses/:id(.:format)                           responses#update
                           PUT    /posts/:post_id/responses/:id(.:format)                           responses#update
                           DELETE /posts/:post_id/responses/:id(.:format)                           responses#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
                           PATCH  /posts/:id(.:format)                                              posts#update
                           PUT    /posts/:id(.:format)                                              posts#update
                           DELETE /posts/:id(.:format)                                              posts#destroy

关注点只是一种重用一组常用路线的方法。有关它的更多信息,请参见documentation

如果你运行rake路线,你应该得到以下路径:

XMLHTTPRequest