2016-06-23 44 views
0

我不知道如何設置多於1個「item」的資源do方法。我有3個腳手架,即Post,Comment,Response。我想達到在耙路線: 'new_post_comment_response => /職位/身份證/評論/ ID /響應/新'ruby​​ on rails 4,設置routes.rb和資源do

的關係是:

有很多

「後型號:評論 有很多:響應

有許多

評價模型:響應 belongs_to的:後

響應模型 belongs_to的:發佈 belongs_to的:發表評論」

在routes.rb中我已設置:

資源:帖子做 資源:評論,除了:[:秀:指數] 結束

資源:備註,請 資源:響應,除了: [:show,:index] end

但是我想在耙路中有三個項目,因爲我要做一些「論壇中的三個響應(評論後的迴應 - 評論)」 。你懂我的意思嗎?

所以,我的問題是:如何設置資源和控制器來檢測Post和Comment ID,以及如何設置資源。

感謝您的幫助!

+0

http://guides.rubyonrails.org/v3.2.9/routing.html#nested-resources你檢查出這個 – Saad

回答

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