【问题标题】:Ruby on rails 4, set routes.rb and resources doRuby on rails 4,设置 routes.rb 和资源
【发布时间】:2016-06-23 14:29:36
【问题描述】:

我不知道如何在资源 do 方法中设置超过 1 个“项目”。我有 3 个脚手架,即 Post、Comment、Response。我想在 rake 路线中达到这一点:'new_post_comment_response => /posts/id/comment/id/response/new'

关系是:

'发布模型 有很多 :cmets 有很多:responses

评论模型 有很多 :responses 属于_to :post

响应模型 属于_to :post 属于_to :comment'

在 routes.rb 我设置了:

资源:帖子做 资源 :cmets,除了:[:show, :index] 结束

资源 :cmets 做 资源 :responses,除了:[:show, :index] 结束

但我想在 rake 路由中包含三个项目,因为我要做一些“论坛中的三个响应(Post-Comment-Response-to-comment)”。你知道我的意思吗?

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

感谢您的帮助!

【问题讨论】:

标签: ruby-on-rails-4 routes


【解决方案1】:

您基本上只需要按照您希望的方式嵌套资源即可。这意味着帖子应该有嵌套的评论和回复,评论应该有嵌套的回复。假设我正确阅读了您的要求,您可能可以按照这个思路进行设置。

concern :responsable do
  resources :responses, except: [:show, :index]
end

resources :posts, concerns: :responsable do
  resources :comments, except: [:show, :index],  concerns: :responsable
end

关注点只是重用一组常用路由的一种方式。更多信息请参阅documentation。

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

    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

【讨论】:

    猜你喜欢
    • 2012-04-21
    • 1970-01-01
    • 2011-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-21
    • 2018-05-25
    • 1970-01-01
    相关资源
    最近更新 更多