Note that there are some explanatory texts on larger screens.

plurals
  1. POShorter Rails routes
    text
    copied!<p>I have a thing blog application, and I would like to shorten my routes. Here there are:</p> <pre><code>Blog::Application.routes.draw do resources :categories do resources :articles do resources :comments end end </code></pre> <p>A <strong>rake routes</strong> command build the following lines:</p> <pre><code> GET /categories/:category_id/articles/:article_id/comments(.:format) {:controller=&gt;"comments", :action=&gt;"index"} category_article_comments POST /categories/:category_id/articles/:article_id/comments(.:format) {:controller=&gt;"comments", :action=&gt;"create"} new_category_article_comment GET /categories/:category_id/articles/:article_id/comments/new(.:format) {:controller=&gt;"comments", :action=&gt;"new"} GET /categories/:category_id/articles/:article_id/comments/:id(.:format) {:controller=&gt;"comments", :action=&gt;"show"} PUT /categories/:category_id/articles/:article_id/comments/:id(.:format) {:controller=&gt;"comments", :action=&gt;"update"} category_article_comment DELETE /categories/:category_id/articles/:article_id/comments/:id(.:format) {:controller=&gt;"comments", :action=&gt;"destroy"} edit_category_article_comment GET /categories/:category_id/articles/:article_id/comments/:id/edit(.:format) {:controller=&gt;"comments", :action=&gt;"edit"} GET /categories/:category_id/articles(.:format) {:controller=&gt;"articles", :action=&gt;"index"} category_articles POST /categories/:category_id/articles(.:format) {:controller=&gt;"articles", :action=&gt;"create"} new_category_article GET /categories/:category_id/articles/new(.:format) {:controller=&gt;"articles", :action=&gt;"new"} GET /categories/:category_id/articles/:id(.:format) {:controller=&gt;"articles", :action=&gt;"show"} PUT /categories/:category_id/articles/:id(.:format) {:controller=&gt;"articles", :action=&gt;"update"} category_article DELETE /categories/:category_id/articles/:id(.:format) {:controller=&gt;"articles", :action=&gt;"destroy"} edit_category_article GET /categories/:category_id/articles/:id/edit(.:format) {:controller=&gt;"articles", :action=&gt;"edit"} GET /categories(.:format) {:controller=&gt;"categories", :action=&gt;"index"} categories POST /categories(.:format) {:controller=&gt;"categories", :action=&gt;"create"} new_category GET /categories/new(.:format) {:controller=&gt;"categories", :action=&gt;"new"} GET /categories/:id(.:format) {:controller=&gt;"categories", :action=&gt;"show"} PUT /categories/:id(.:format) {:controller=&gt;"categories", :action=&gt;"update"} category DELETE /categories/:id(.:format) {:controller=&gt;"categories", :action=&gt;"destroy"} edit_category GET /categories/:id/edit(.:format) {:controller=&gt;"categories", :action=&gt;"edit"} </code></pre> <p>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:</p> <pre><code>/categories/ =&gt; / /categories/:id =&gt; /:id /categories/:category_id/articles/ =&gt; /:category_id/articles /categories/:category_id/articles/:id =&gt; /:category_id/:id /categories/:category_id/articles/:article_id/comments/ =&gt; /:category_id/:article_id/comments /categories/:category_id/articles/:article_id/comments/:id =&gt; /:category_id/:article_id/:id </code></pre> <p>It's more DRY, is't it? :)</p> <p>Does Rails 3 provides a easy way to do so, with an HTTP verbs mapping to controller actions automatically? Thanks anyone.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload