# Shorter Rails 3 routes

**URL:** <https://www.ruby-forum.com/t/shorter-rails-3-routes/185861>\
**Category:** Rails\
**Created:** [March 27, 2010, 7:13pm UTC](https://www.ruby-forum.com/t/shorter-rails-3-routes/185861 "2010-03-27T19:13:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![judge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@judge](https://www.ruby-forum.com/u/judge)\
**Post date:** [March 27, 2010, 7:13pm UTC](https://www.ruby-forum.com/t/shorter-rails-3-routes/185861/1 "2010-03-27T19:13:17Z")

</div>

Hi,

I have a small blog application, and I would like to shorten its routes.  
Here they are:

Blog::Application.routes.draw do  
resources :categories do  
resources :articles do  
resources :comments  
end  
end

A rake routes command produce the following lines:

```
                          GET

```

/categories/:category\_id/articles/:article\_id/comments(.:format)  
{:controller=\>“comments”, :action=\>“index”}  
category\_article\_comments POST  
/categories/:category\_id/articles/:article\_id/comments(.:format)  
{:controller=\>“comments”, :action=\>“create”}  
new\_category\_article\_comment GET  
/categories/:category\_id/articles/:article\_id/comments/new(.:format)  
{:controller=\>“comments”, :action=\>“new”}  
GET  
/categories/:category\_id/articles/:article\_id/comments/:id(.:format)  
{:controller=\>“comments”, :action=\>“show”}  
PUT  
/categories/:category\_id/articles/:article\_id/comments/:id(.:format)  
{:controller=\>“comments”, :action=\>“update”}  
category\_article\_comment DELETE  
/categories/:category\_id/articles/:article\_id/comments/:id(.:format)  
{:controller=\>“comments”, :action=\>“destroy”}  
edit\_category\_article\_comment GET  
/categories/:category\_id/articles/:article\_id/comments/:id/edit(.:format)  
{:controller=\>“comments”, :action=\>“edit”}  
GET  
/categories/:category\_id/articles(.:format)  
{:controller=\>“articles”, :action=\>“index”}  
category\_articles POST  
/categories/:category\_id/articles(.:format)  
{:controller=\>“articles”, :action=\>“create”}  
new\_category\_article GET  
/categories/:category\_id/articles/new(.:format)  
{:controller=\>“articles”, :action=\>“new”}  
GET  
/categories/:category\_id/articles/:id(.:format)  
{:controller=\>“articles”, :action=\>“show”}  
PUT  
/categories/:category\_id/articles/:id(.:format)  
{:controller=\>“articles”, :action=\>“update”}  
category\_article DELETE  
/categories/:category\_id/articles/:id(.:format)  
{:controller=\>“articles”, :action=\>“destroy”}  
edit\_category\_article GET  
/categories/:category\_id/articles/:id/edit(.:format)  
{:controller=\>“articles”, :action=\>“edit”}  
GET /categories(.:format)  
{:controller=\>“categories”, :action=\>“index”}  
categories POST /categories(.:format)  
{:controller=\>“categories”, :action=\>“create”}  
new\_category GET /categories/new(.:format)  
{:controller=\>“categories”, :action=\>“new”}  
GET /categories/:id(.:format)  
{:controller=\>“categories”, :action=\>“show”}  
PUT /categories/:id(.:format)  
{:controller=\>“categories”, :action=\>“update”}  
category DELETE /categories/:id(.:format)  
{:controller=\>“categories”, :action=\>“destroy”}  
edit\_category GET /categories/:id/edit(.:format)  
{:controller=\>“categories”, :action=\>“edit”}

As can be seen, each resource is ordered in a tree. So I believe that,  
it’s could be interesting to simplify my routes such as for example:

/categories/ =\> /  
/categories/:id =\> /:id  
/categories/:category\_id/articles/ =\>  
/:category\_id/articles  
/categories/:category\_id/articles/:id =\>  
/:category\_id/:id  
/categories/:category\_id/articles/:article\_id/comments/ =\>  
/:category\_id/:article\_id/comments  
/categories/:category\_id/articles/:article\_id/comments/:id =\>  
/:category\_id/:article\_id/:id

It’s more DRY, is’t it? 🙂

Does Rails 3 provides a easy way to do so, with an HTTP verbs mapping to  
controller actions automatically?

Thanks anyone.

---

<div class="post-metadata">

**Author:** ![judge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@judge](https://www.ruby-forum.com/u/judge)\
**Post date:** [March 27, 2010, 7:24pm UTC](https://www.ruby-forum.com/t/shorter-rails-3-routes/185861/2 "2010-03-27T19:24:26Z")

</div>

Hi Paul,

Take a look at shallow nesting:

> **[rails.info](http://guides.rails.info/routing.html#shallow-nesting)**
>
> Ruby on Rails,Rails Tutorial,Ruby Basics,Rails 4,Bar Rails,Rail

Hope this helps.

/Lasse

2010/3/27 Paul A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![judge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@judge](https://www.ruby-forum.com/u/judge)\
**Post date:** [March 27, 2010, 7:54pm UTC](https://www.ruby-forum.com/t/shorter-rails-3-routes/185861/3 "2010-03-27T19:54:14Z")

</div>

Lasse B. wrote:

> Hi Paul,
> 
> Take a look at shallow nesting:  
> [rails.info](http://guides.rails.info/routing.html#shallow-nesting)
> 
> Hope this helps.
> 
> /Lasse
> 
> 2010/3/27 Paul A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

I think it solves my issue.

Thanks
