سؤال

I believe we are all familiar with the normal posts and comments models to be displayed. Now imagine those relations hold. I would like to add replies to my comments so we have routing as follows

resources :posts do
    resources :comments do
        resources :replies do
    end
end

I tried many different ways of implementing this in my views but I had no luck! It just does not work when my actual controller is post and I want to access replies. I tried searching for this but did not find the suitable name for it. Is there any resource that have information about how to implement this system or a code snippet on how to make it work on n levels instead of just 2?

هل كانت مفيدة؟

المحلول

http://nithinbekal.com/posts/rails-shallow-nesting/

You can use shallow nesting instead.

resources :posts, shallow: true do
    resources :comments do
        resources :replies do
    end
end

This way you only have a nested route when you need to create a nested object [new and create action] or see all the related objects [index view]. Look at the first three lines below to see what i mean. And then you have normal routes for the resources that dont need to know the relation.

     comment_replies GET    /comments/:comment_id/replies(.:format)         replies#index
                     POST   /comments/:comment_id/replies(.:format)         replies#create
   new_comment_reply GET    /comments/:comment_id/replies/new(.:format)     replies#new
          edit_reply GET    /replies/:id/edit(.:format)                     replies#edit
               reply GET    /replies/:id(.:format)                          replies#show
                     PATCH  /replies/:id(.:format)                          replies#update
                     PUT    /replies/:id(.:format)                          replies#update
                     DELETE /replies/:id(.:format)                          replies#destroy
       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_comment GET    /comments/:id/edit(.:format)                    comments#edit
             comment GET    /comments/:id(.:format)                         comments#show
                     PATCH  /comments/:id(.:format)                         comments#update
                     PUT    /comments/:id(.:format)                         comments#update
                     DELETE /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
                     PATCH  /posts/:id(.:format)                            posts#update
                     PUT    /posts/:id(.:format)                            posts#update
                     DELETE /posts/:id(.:format) 

نصائح أخرى

Rule of thumb:

Resources should never be nested more than 1 level deep.

I suggest you to read this article by Jamis Buck. You probably want to do something like:

resources :posts do
  resources :comments
end

resources :comments
  resources :replies
end
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top