Haseeb Ahmad Haseeb Ahmad - 4 months ago 29
Ruby Question

What is the difference between resources and namespace in rails nested routing

What is the difference between

namespace :alpha do
resources :posts
end


and

resources :alpha do
resources :posts
end

Answer

Check out the difference using rake routes.

This definition with a namespace:

namespace :alpha do
  resources :posts
end

results in the following routes:

         Prefix Verb   URI Pattern                     Controller#Action
    alpha_posts GET    /alpha/posts(.:format)          alpha/posts#index
                POST   /alpha/posts(.:format)          alpha/posts#create
 new_alpha_post GET    /alpha/posts/new(.:format)      alpha/posts#new
edit_alpha_post GET    /alpha/posts/:id/edit(.:format) alpha/posts#edit
     alpha_post GET    /alpha/posts/:id(.:format)      alpha/posts#show
                PATCH  /alpha/posts/:id(.:format)      alpha/posts#update
                PUT    /alpha/posts/:id(.:format)      alpha/posts#update
                DELETE /alpha/posts/:id(.:format)      alpha/posts#destroy

As you can see, the only thing that is different from a plain resources route set is the addition of /alpha prefix.

Now for the two-level resources routes definition:

resources :alpha do
  resources :posts
end

which results in:

         Prefix Verb   URI Pattern                               Controller#Action
    alpha_posts GET    /alpha/:alpha_id/posts(.:format)          posts#index
                POST   /alpha/:alpha_id/posts(.:format)          posts#create
 new_alpha_post GET    /alpha/:alpha_id/posts/new(.:format)      posts#new
edit_alpha_post GET    /alpha/:alpha_id/posts/:id/edit(.:format) posts#edit
     alpha_post GET    /alpha/:alpha_id/posts/:id(.:format)      posts#show
                PATCH  /alpha/:alpha_id/posts/:id(.:format)      posts#update
                PUT    /alpha/:alpha_id/posts/:id(.:format)      posts#update
                DELETE /alpha/:alpha_id/posts/:id(.:format)      posts#destroy
    alpha_index GET    /alpha(.:format)                          alpha#index
                POST   /alpha(.:format)                          alpha#create
      new_alpha GET    /alpha/new(.:format)                      alpha#new
     edit_alpha GET    /alpha/:id/edit(.:format)                 alpha#edit
          alpha GET    /alpha/:id(.:format)                      alpha#show
                PATCH  /alpha/:id(.:format)                      alpha#update
                PUT    /alpha/:id(.:format)                      alpha#update
                DELETE /alpha/:id(.:format)                      alpha#destroy

As you can see, alpha becomes a top-level resource with all 8 RESTful routes. posts, in turn, become second-level resource, accessible only through the route to a specific alpha object.

Read more in Rails Routing from the Outside In.

You might also find interesting the scope option. Read about the difference between scope and namespace in Scoping Rails Routes blog post.

Comments