ddonche
ddonche

Reputation: 1065

Rails nested routes not working as expected

I have nested resources in my routes like so. These work perfectly on my other rails 5 app, but not on my rails 6 app. I cannot figure out why it recognizes only the first level of nested stuff.

  resources :blogs do
    member do
      put 'like', to: 'blogs#upvote'
      put 'dislike', to: 'blogs#downvote'
    end
    resources :comments
      member do
        put 'like', to: 'comments#upvote'
        put 'dislike', to: 'comments#downvote'
      end
      resources :notations
  end

Here is what rake routes gives me:

 blogs_user GET      /users/:id/blogs(.:format)                                                               users#blogs
                             like_blog PUT      /blogs/:id/like(.:format)                                                                blogs#upvote
                          dislike_blog PUT      /blogs/:id/dislike(.:format)                                                             blogs#downvote
                         blog_comments GET      /blogs/:blog_id/comments(.:format)                                                       comments#index
                                       POST     /blogs/:blog_id/comments(.:format)                                                       comments#create
                      new_blog_comment GET      /blogs/:blog_id/comments/new(.:format)                                                   comments#new
                     edit_blog_comment GET      /blogs/:blog_id/comments/:id/edit(.:format)                                              comments#edit
                          blog_comment GET      /blogs/:blog_id/comments/:id(.:format)                                                   comments#show
                                       PATCH    /blogs/:blog_id/comments/:id(.:format)                                                   comments#update
                                       PUT      /blogs/:blog_id/comments/:id(.:format)                                                   comments#update
                                       DELETE   /blogs/:blog_id/comments/:id(.:format)                                                   comments#destroy
                                       PUT      /blogs/:id/like(.:format)                                                                comments#upvote
                                       PUT      /blogs/:id/dislike(.:format)                                                             comments#downvote
                             notations GET      /blogs/:id/notations(.:format)                                                           notations#index
                                       POST     /blogs/:id/notations(.:format)                                                           notations#create
                          new_notation GET      /blogs/:id/notations/new(.:format)                                                       notations#new
                         edit_notation GET      /blogs/:id/notations/:id/edit(.:format)                                                  notations#edit
                              notation GET      /blogs/:id/notations/:id(.:format)                                                       notations#show
                                       PATCH    /blogs/:id/notations/:id(.:format)                                                       notations#update
                                       PUT      /blogs/:id/notations/:id(.:format)                                                       notations#update
                                       DELETE   /blogs/:id/notations/:id(.:format)                                                       notations#destroy

On my other app, for example, it would produce

/blogs/:blog_id/comments/:id/like

Upvotes: 0

Views: 125

Answers (2)

Gustavo Toro
Gustavo Toro

Reputation: 450

I make a copy of your routes and replicated in two apps (Rails 5 and Rails 6) and both produced same routes (without three nested level). If you want the /blogs/:blog_id/comments/:id/like route, you must do a small change.

  resources :blogs do
    member do
      put 'like', to: 'blogs#upvote'
      put 'dislike', to: 'blogs#downvote'
    end
    resources :comments do
      member do
        put 'like', to: 'comments#upvote'
        put 'dislike', to: 'comments#downvote'
      end
    end
    resources :notations
  end

Upvotes: 1

arieljuod
arieljuod

Reputation: 15838

You are missing the "do" "end" block syntax

resources :blogs do
  member do
    put 'like', to: 'blogs#upvote'
    put 'dislike', to: 'blogs#downvote'
  end
  resources :comments do # here
    member do
      put 'like', to: 'comments#upvote'
      put 'dislike', to: 'comments#downvote'
    end
    resources :notations
  end # and here
end

Anyway, more than two levels of nesting is discouraged by the rails guidelines.

Upvotes: 1

Related Questions