资源路由:简短的风格

时间:2011-08-31 13:38:17

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

这是我的config/routes.rb

Eesoudayo::Application.routes.draw do
  resources :articles do
    resources :comments
  end
end

......正如您可以想象的那样,生成以下路线:

    article_comments GET    /articles/:article_id/comments(.:format)          {:action=>"index", :controller=>"comments"}
                     POST   /articles/:article_id/comments(.:format)          {:action=>"create", :controller=>"comments"}
 new_article_comment GET    /articles/:article_id/comments/new(.:format)      {:action=>"new", :controller=>"comments"}
edit_article_comment GET    /articles/:article_id/comments/:id/edit(.:format) {:action=>"edit", :controller=>"comments"}
     article_comment GET    /articles/:article_id/comments/:id(.:format)      {:action=>"show", :controller=>"comments"}
                     PUT    /articles/:article_id/comments/:id(.:format)      {:action=>"update", :controller=>"comments"}
                     DELETE /articles/:article_id/comments/:id(.:format)      {:action=>"destroy", :controller=>"comments"}
            articles GET    /articles(.:format)                               {:action=>"index", :controller=>"articles"}
                     POST   /articles(.:format)                               {:action=>"create", :controller=>"articles"}
         new_article GET    /articles/new(.:format)                           {:action=>"new", :controller=>"articles"}
        edit_article GET    /articles/:id/edit(.:format)                      {:action=>"edit", :controller=>"articles"}
             article GET    /articles/:id(.:format)                           {:action=>"show", :controller=>"articles"}
                     PUT    /articles/:id(.:format)                           {:action=>"update", :controller=>"articles"}
                     DELETE /articles/:id(.:format)                           {:action=>"destroy", :controller=>"articles"}

我的问题是:是否有一种性感的方式来清理它们,给出这个结果?

    article_comments GET    /:article_id/(.:format)         {:action=>"index", :controller=>"comments"}
                     POST   /:article_id/(.:format)         {:action=>"create", :controller=>"comments"}
 new_article_comment GET    /:article_id/new(.:format)      {:action=>"new", :controller=>"comments"}
edit_article_comment GET    /:article_id/:id/edit(.:format) {:action=>"edit", :controller=>"comments"}
     article_comment GET    /:article_id/:id(.:format)      {:action=>"show", :controller=>"comments"}
                     PUT    /:article_id/:id(.:format)      {:action=>"update", :controller=>"comments"}
                     DELETE /:article_id/:id(.:format)      {:action=>"destroy", :controller=>"comments"}
            articles GET    /.:format)                      {:action=>"index", :controller=>"articles"}
                     POST   /.:format)                      {:action=>"create", :controller=>"articles"}
         new_article GET    /new(.:format)                  {:action=>"new", :controller=>"articles"}
        edit_article GET    /:id/edit(.:format)             {:action=>"edit", :controller=>"articles"}
             article GET    /:id(.:format)                  {:action=>"show", :controller=>"articles"}
                     PUT    /:id(.:format)                  {:action=>"update", :controller=>"articles"}
                     DELETE /:id(.:format)                  {:action=>"destroy", :controller=>"articles"}

PS:我正在使用Rails 3.x< 3

1 个答案:

答案 0 :(得分:1)

我真的不知道这是否有效,真是在黑暗中拍摄:

Eesoudayo::Application.routes.draw do
  resources :articles, :path => "/" do
    resources :comments, :path => "/"
  end
end

我只使用rake routes对此进行了测试,当您尝试启动服务器时,一切都会爆炸。