Re: Shorter Rails 3 routes

Lasse Bunk
Sat, 27 Mar 2010 18:23:08 -0000

Hi Paul,

Take a look at shallow nesting:
http://guides.rails.info/routing.html#shallow-nesting

Hope this helps.

/Lasse

2010/3/27 Paul A. <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org>

> 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.
> --
> Posted via http://www.ruby-forum.com/.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
> To unsubscribe from this group, send email to
>
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
> .
> For more options, visit this group at
> http://groups.google.com/group/rubyonrails-talk?hl=en.
>
>