由slug命名的奇异资源(路由)

时间:2010-08-15 11:46:44

标签: ruby-on-rails routing routes url-routing

我有跟随Rails 3的路线:

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

通过耙他们,我们可以找到:

                          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"}

因为每条路径都以相同的根路径( / blogs )开头,所以我希望通过删除它来缩短地址(在给出blog_id的时候)。

在这个原因中,我可以选择路线(我认为它更干):

                          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"}

根据你的说法,我应该对当前的路线配置做出什么样的改变?

感谢。

2 个答案:

答案 0 :(得分:0)

我的意见是你需要看看浅薄的路线。据我所知,使用路由资源无法实现您所要求的定制,我认为这是不可取的。

如果已定义了一对多关系,则无需为每个嵌套资源传递标识符。这被认为是不好的做法(有些人,但不是其他人)。例如,而不是这个长网址:

/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]

你实际上只需要:

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

更多信息: http://railscasts.com/episodes/139-nested-resources

答案 1 :(得分:0)

“资源不应该嵌套超过1级。” (http://guides.rubyonrails.org/routing.html)

如果Rails文档强烈推荐,我不知道为什么它仍然被允许。