views:

49

answers:

1

Hi,

I have the followed Rails 3 routes:

Hello::Application.routes.draw do
  resources :blogs do
    resources :articles do
      resources :comments
    end
  end
end

By raking them, we can find:

                          GET    /blogs/:blog_id/articles/:article_id/comments(.:format)          {:action=>"index", :controller=>"comments"}
    blog_article_comments POST   /blogs/:blog_id/articles/:article_id/comments(.:format)          {:action=>"create", :controller=>"comments"}
 new_blog_article_comment GET    /blogs/:blog_id/articles/:article_id/comments/new(.:format)      {:action=>"new", :controller=>"comments"}
                          GET    /blogs/:blog_id/articles/:article_id/comments/:id(.:format)      {:action=>"show", :controller=>"comments"}
                          PUT    /blogs/:blog_id/articles/:article_id/comments/:id(.:format)      {:action=>"update", :controller=>"comments"}
     blog_article_comment DELETE /blogs/:blog_id/articles/:article_id/comments/:id(.:format)      {:action=>"destroy", :controller=>"comments"}
edit_blog_article_comment GET    /blogs/:blog_id/articles/:article_id/comments/:id/edit(.:format) {:action=>"edit", :controller=>"comments"}
                          GET    /blogs/:blog_id/articles(.:format)                               {:action=>"index", :controller=>"articles"}
            blog_articles POST   /blogs/:blog_id/articles(.:format)                               {:action=>"create", :controller=>"articles"}
         new_blog_article GET    /blogs/:blog_id/articles/new(.:format)                           {:action=>"new", :controller=>"articles"}
                          GET    /blogs/:blog_id/articles/:id(.:format)                           {:action=>"show", :controller=>"articles"}
                          PUT    /blogs/:blog_id/articles/:id(.:format)                           {:action=>"update", :controller=>"articles"}
             blog_article DELETE /blogs/:blog_id/articles/:id(.:format)                           {:action=>"destroy", :controller=>"articles"}
        edit_blog_article GET    /blogs/:blog_id/articles/:id/edit(.:format)                      {:action=>"edit", :controller=>"articles"}
                          GET    /blogs(.:format)                                                 {:action=>"index", :controller=>"blogs"}
                    blogs POST   /blogs(.:format)                                                 {:action=>"create", :controller=>"blogs"}
                 new_blog GET    /blogs/new(.:format)                                             {:action=>"new", :controller=>"blogs"}
                          GET    /blogs/:id(.:format)                                             {:action=>"show", :controller=>"blogs"}
                          PUT    /blogs/:id(.:format)                                             {:action=>"update", :controller=>"blogs"}
                     blog DELETE /blogs/:id(.:format)                                             {:action=>"destroy", :controller=>"blogs"}
                edit_blog GET    /blogs/:id/edit(.:format)                                        {:action=>"edit", :controller=>"blogs"}

Because every routes begin with the same root path (/blogs), I would like to shorten addresses by removing it (when :blog_id is given).

In this why, I could have thoses routes (I think it's more DRY):

                          GET    /:blog_id/articles/:article_id/comments(.:format)          {:action=>"index", :controller=>"comments"}
    blog_article_comments POST   /:blog_id/articles/:article_id/comments(.:format)          {:action=>"create", :controller=>"comments"}
 new_blog_article_comment GET    /:blog_id/articles/:article_id/comments/new(.:format)      {:action=>"new", :controller=>"comments"}
                          GET    /:blog_id/articles/:article_id/comments/:id(.:format)      {:action=>"show", :controller=>"comments"}
                          PUT    /:blog_id/articles/:article_id/comments/:id(.:format)      {:action=>"update", :controller=>"comments"}
     blog_article_comment DELETE /:blog_id/articles/:article_id/comments/:id(.:format)      {:action=>"destroy", :controller=>"comments"}
edit_blog_article_comment GET    /:blog_id/articles/:article_id/comments/:id/edit(.:format) {:action=>"edit", :controller=>"comments"}
                          GET    /:blog_id/articles(.:format)                               {:action=>"index", :controller=>"articles"}
            blog_articles POST   /:blog_id/articles(.:format)                               {:action=>"create", :controller=>"articles"}
         new_blog_article GET    /:blog_id/articles/new(.:format)                           {:action=>"new", :controller=>"articles"}
                          GET    /:blog_id/articles/:id(.:format)                           {:action=>"show", :controller=>"articles"}
                          PUT    /:blog_id/articles/:id(.:format)                           {:action=>"update", :controller=>"articles"}
             blog_article DELETE /:blog_id/articles/:id(.:format)                           {:action=>"destroy", :controller=>"articles"}
        edit_blog_article GET    /:blog_id/articles/:id/edit(.:format)                      {:action=>"edit", :controller=>"articles"}
                          GET    /blogs(.:format)                                                 {:action=>"index", :controller=>"blogs"}
                    blogs POST   /blogs(.:format)                                                 {:action=>"create", :controller=>"blogs"}
                 new_blog GET    /blogs/new(.:format)                                             {:action=>"new", :controller=>"blogs"}
                          GET    /blogs/:id(.:format)                                             {:action=>"show", :controller=>"blogs"}
                          PUT    /blogs/:id(.:format)                                             {:action=>"update", :controller=>"blogs"}
                     blog DELETE /blogs/:id(.:format)                                             {:action=>"destroy", :controller=>"blogs"}
                edit_blog GET    /blogs/:id/edit(.:format)                                        {:action=>"edit", :controller=>"blogs"}

According to you, what kind of change I should make over my current routes configuration?

Thanks.

A: 

My opinion is that you need to look at shallow routes. To my knowledge the customisation you are asking is not achievable using route resources and I don't think it's desirable.

Where you have defined one to many relationships you do not need to pass the identifier for each of the nested resources. This is considered bad practice (by some, though not others). For example, instead of this long url:

/blog/:blog_id/articles/:article_id/comments/:id
#controller
@blog = Blog.find params[:blog_id]
@article = Blog.find params[:article_id]
@comment = Comment.find params[:id]

you actually need only:

/comments/:id
#controller
@comment = Comment.find params[:id]
@article = @comment.article
@blog = @article.blog

More info: http://railscasts.com/episodes/139-nested-resources

mark
Just noticed you're using rails 3 but I assume not so much has changed.
mark
Yes, but now shallow is over with rails 3.
moshimoshi
What do you mean by 'over'? It just hasn't been implemented yet in the release candidate you've chose to use. https://rails.lighthouseapp.com/projects/8994/tickets/3765. My answer still stands. You could define named shallow routes or use 2.3 with shallow nesting defined on resources.
mark