较短的Rails路线

时间:2010-03-27 15:00:05

标签: ruby routes ruby-on-rails-3

我有一个博客应用程序,我想缩短我的路线。这里有:

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

rake routes 命令构建以下行:

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

可以看出,每个资源都在树中排序。 所以我相信,简化我的路线可能会很有趣,例如:

/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

干得多,不是吗? :)

Rails 3是否提供了一种简单的方法,HTTP动词会自动映射到控制器操作?谢谢任何人。

1 个答案:

答案 0 :(得分:0)

看看shallow nesting

希望这有帮助。