How to Use Nested Routes in Rails: Syntax and Examples
In Rails, you use
nested routes by placing one resource block inside another in the config/routes.rb file. This creates URLs that reflect the parent-child relationship, like /parents/:parent_id/children/:id, helping organize related resources clearly.Syntax
Nested routes are defined by placing a resources block inside another resources block in config/routes.rb. The outer resource is the parent, and the inner resource is the child. This creates URLs that include the parent's ID, showing the relationship.
resources :parentsdefines routes for the parent resource.- Inside it,
resources :childrendefines routes for the child resource nested under the parent. - The URL pattern becomes
/parents/:parent_id/children/:id.
ruby
Rails.application.routes.draw do
resources :parents do
resources :children
end
endExample
This example shows nested routes for articles and their comments. Comments belong to articles, so their routes are nested. This creates URLs like /articles/1/comments/5 to access comment 5 of article 1.
ruby
Rails.application.routes.draw do
resources :articles do
resources :comments
end
endOutput
Prefix Verb URI Pattern Controller#Action
article_comments GET /articles/:article_id/comments(.:format) comments#index
POST /articles/:article_id/comments(.:format) comments#create
new_article_comment GET /articles/:article_id/comments/new(.:format) comments#new
edit_article_comment GET /articles/:article_id/comments/:id/edit(.:format) comments#edit
article_comment GET /articles/:article_id/comments/:id(.:format) comments#show
PATCH /articles/:article_id/comments/:id(.:format) comments#update
PUT /articles/:article_id/comments/:id(.:format) comments#update
DELETE /articles/:article_id/comments/:id(.:format) comments#destroy
articles GET /articles(.:format) articles#index
POST /articles(.:format) articles#create
new_article GET /articles/new(.:format) articles#new
edit_article GET /articles/:id/edit(.:format) articles#edit
article GET /articles/:id(.:format) articles#show
PATCH /articles/:id(.:format) articles#update
PUT /articles/:id(.:format) articles#update
DELETE /articles/:id(.:format) articles#destroy
Common Pitfalls
Common mistakes when using nested routes include:
- Over-nesting resources, which makes URLs long and complex.
- Not passing the parent resource ID in controller actions, causing errors.
- Using shallow nesting incorrectly, which can confuse URL helpers.
Always keep nesting to 1 or 2 levels deep for clarity and maintainability.
ruby
Rails.application.routes.draw do
# Wrong: too deep nesting
resources :countries do
resources :cities do
resources :streets
end
end
# Right: shallow nesting for streets
resources :countries do
resources :cities do
resources :streets, shallow: true
end
end
endQuick Reference
Tips for nested routes in Rails:
- Use
resourcesinside anotherresourcesto nest. - Limit nesting depth to 1 or 2 levels.
- Use
shallow: trueto simplify nested routes for child resources. - Access parent IDs in controllers via
params[:parent_id]. - Use URL helpers like
parent_child_path(parent, child)for links.
Key Takeaways
Define nested routes by placing one resources block inside another in config/routes.rb.
Nested routes create URLs that reflect parent-child relationships, like /parents/:parent_id/children/:id.
Avoid deep nesting to keep URLs simple and maintainable.
Use shallow nesting to reduce URL complexity for deeply nested resources.
Access parent resource IDs in controllers via params[:parent_id] to link related data.