浅层嵌套Rails路由:as

时间:2009-11-28 15:55:48

标签: ruby-on-rails routing

我想:

  • 每个项目部分属于一个 项目

  • 每个解决方案属于一个 projectart(以及通过的项目) 那个项目部分。

  • 每张图片都属于一个解决方案 (以及项目和项目部分 通过该解决方案。)

  • 每个文件属于一个 解决方案(和项目和 projectpart通过该解决方案。)

  • 每个网址都像简单一样简短 可能的。

  • 出现“projectpart”的每个案例 作为每个URL中的“部分”。 (我不能 在Heroku上调用模型“part”。)

谁能告诉我为什么这样......

ActionController::Routing::Routes.draw do |map|
  map.resources :projects, :shallow => true do |project|
    project.resources :projectparts do |part|
      part.resources :solutions do |solution|
        solution.resources :images
        solution.resources :documents
      end
    end
  end
  map.connect ':controller/:action/:id'
  map.connect ':controller/:action/:id.:format'
end

...在我的网址中留下了一大堆“projectpart”案例......

steven-nobles-imac-200:drominay steven$ rake routes
(in /Users/steven/Drominay)

                          projects GET    /projects                                           {:controller=>"projects", :action=>"index"}
                formatted_projects GET    /projects.:format                                   {:controller=>"projects", :action=>"index"}
                                   POST   /projects                                           {:controller=>"projects", :action=>"create"}
                                   POST   /projects.:format                                   {:controller=>"projects", :action=>"create"}
                       new_project GET    /projects/new                                       {:controller=>"projects", :action=>"new"}
             formatted_new_project GET    /projects/new.:format                               {:controller=>"projects", :action=>"new"}
                      edit_project GET    /projects/:id/edit                                  {:controller=>"projects", :action=>"edit"}
            formatted_edit_project GET    /projects/:id/edit.:format                          {:controller=>"projects", :action=>"edit"}
                           project GET    /projects/:id                                       {:controller=>"projects", :action=>"show"}
                 formatted_project GET    /projects/:id.:format                               {:controller=>"projects", :action=>"show"}
                                   PUT    /projects/:id                                       {:controller=>"projects", :action=>"update"}
                                   PUT    /projects/:id.:format                               {:controller=>"projects", :action=>"update"}
                                   DELETE /projects/:id                                       {:controller=>"projects", :action=>"destroy"}
                                   DELETE /projects/:id.:format                               {:controller=>"projects", :action=>"destroy"}
              project_projectparts GET    /projects/:project_id/projectparts                  {:controller=>"projectparts", :action=>"index"}
    formatted_project_projectparts GET    /projects/:project_id/projectparts.:format          {:controller=>"projectparts", :action=>"index"}
                                   POST   /projects/:project_id/projectparts                  {:controller=>"projectparts", :action=>"create"}
                                   POST   /projects/:project_id/projectparts.:format          {:controller=>"projectparts", :action=>"create"}
           new_project_projectpart GET    /projects/:project_id/projectparts/new              {:controller=>"projectparts", :action=>"new"}
 formatted_new_project_projectpart GET    /projects/:project_id/projectparts/new.:format      {:controller=>"projectparts", :action=>"new"}
                  edit_projectpart GET    /projectparts/:id/edit                              {:controller=>"projectparts", :action=>"edit"}
        formatted_edit_projectpart GET    /projectparts/:id/edit.:format                      {:controller=>"projectparts", :action=>"edit"}
                       projectpart GET    /projectparts/:id                                   {:controller=>"projectparts", :action=>"show"}
             formatted_projectpart GET    /projectparts/:id.:format                           {:controller=>"projectparts", :action=>"show"}
                                   PUT    /projectparts/:id                                   {:controller=>"projectparts", :action=>"update"}
                                   PUT    /projectparts/:id.:format                           {:controller=>"projectparts", :action=>"update"}
                                   DELETE /projectparts/:id                                   {:controller=>"projectparts", :action=>"destroy"}
                                   DELETE /projectparts/:id.:format                           {:controller=>"projectparts", :action=>"destroy"}
             projectpart_solutions GET    /projectparts/:projectpart_id/solutions             {:controller=>"solutions", :action=>"index"}
   formatted_projectpart_solutions GET    /projectparts/:projectpart_id/solutions.:format     {:controller=>"solutions", :action=>"index"}
                                   POST   /projectparts/:projectpart_id/solutions             {:controller=>"solutions", :action=>"create"}
                                   POST   /projectparts/:projectpart_id/solutions.:format     {:controller=>"solutions", :action=>"create"}
          new_projectpart_solution GET    /projectparts/:projectpart_id/solutions/new         {:controller=>"solutions", :action=>"new"}
formatted_new_projectpart_solution GET    /projectparts/:projectpart_id/solutions/new.:format {:controller=>"solutions", :action=>"new"}
                     edit_solution GET    /solutions/:id/edit                                 {:controller=>"solutions", :action=>"edit"}
           formatted_edit_solution GET    /solutions/:id/edit.:format                         {:controller=>"solutions", :action=>"edit"}
                          solution GET    /solutions/:id                                      {:controller=>"solutions", :action=>"show"}
                formatted_solution GET    /solutions/:id.:format                              {:controller=>"solutions", :action=>"show"}
                                   PUT    /solutions/:id                                      {:controller=>"solutions", :action=>"update"}
                                   PUT    /solutions/:id.:format                              {:controller=>"solutions", :action=>"update"}
                                   DELETE /solutions/:id                                      {:controller=>"solutions", :action=>"destroy"}
                                   DELETE /solutions/:id.:format                              {:controller=>"solutions", :action=>"destroy"}
                   solution_images GET    /solutions/:solution_id/images                      {:controller=>"images", :action=>"index"}
         formatted_solution_images GET    /solutions/:solution_id/images.:format              {:controller=>"images", :action=>"index"}
                                   POST   /solutions/:solution_id/images                      {:controller=>"images", :action=>"create"}
                                   POST   /solutions/:solution_id/images.:format              {:controller=>"images", :action=>"create"}
                new_solution_image GET    /solutions/:solution_id/images/new                  {:controller=>"images", :action=>"new"}
      formatted_new_solution_image GET    /solutions/:solution_id/images/new.:format          {:controller=>"images", :action=>"new"}
                        edit_image GET    /images/:id/edit                                    {:controller=>"images", :action=>"edit"}
              formatted_edit_image GET    /images/:id/edit.:format                            {:controller=>"images", :action=>"edit"}
                             image GET    /images/:id                                         {:controller=>"images", :action=>"show"}
                   formatted_image GET    /images/:id.:format                                 {:controller=>"images", :action=>"show"}
                                   PUT    /images/:id                                         {:controller=>"images", :action=>"update"}
                                   PUT    /images/:id.:format                                 {:controller=>"images", :action=>"update"}
                                   DELETE /images/:id                                         {:controller=>"images", :action=>"destroy"}
                                   DELETE /images/:id.:format                                 {:controller=>"images", :action=>"destroy"}
                solution_documents GET    /solutions/:solution_id/documents                   {:controller=>"documents", :action=>"index"}
      formatted_solution_documents GET    /solutions/:solution_id/documents.:format           {:controller=>"documents", :action=>"index"}
                                   POST   /solutions/:solution_id/documents                   {:controller=>"documents", :action=>"create"}
                                   POST   /solutions/:solution_id/documents.:format           {:controller=>"documents", :action=>"create"}
             new_solution_document GET    /solutions/:solution_id/documents/new               {:controller=>"documents", :action=>"new"}
   formatted_new_solution_document GET    /solutions/:solution_id/documents/new.:format       {:controller=>"documents", :action=>"new"}
                     edit_document GET    /documents/:id/edit                                 {:controller=>"documents", :action=>"edit"}
           formatted_edit_document GET    /documents/:id/edit.:format                         {:controller=>"documents", :action=>"edit"}
                          document GET    /documents/:id                                      {:controller=>"documents", :action=>"show"}
                formatted_document GET    /documents/:id.:format                              {:controller=>"documents", :action=>"show"}
                                   PUT    /documents/:id                                      {:controller=>"documents", :action=>"update"}
                                   PUT    /documents/:id.:format                              {:controller=>"documents", :action=>"update"}
                                   DELETE /documents/:id                                      {:controller=>"documents", :action=>"destroy"}
                                   DELETE /documents/:id.:format                              {:controller=>"documents", :action=>"destroy"}
                                          /:controller/:action/:id                            
                                          /:controller/:action/:id.:format   

...但是还没有像这样生成基本的URL帮助器?

undefined method `project_projectpart_path' for #<ActionView::Base:0x3438ffc> (ActionView::TemplateError)
顺便说一句,除了将每个URL中的“projectpart”更改为“part”之外的所有内容都使用这种更详细的语法正常工作:

ActionController::Routing::Routes.draw do |map|


  map.resources :projects, :has_many => :projectparts
  map.resources :projects, :has_many => :solutions
  map.resources :projects, :has_many => :images
  map.resources :projects, :has_many => :documents
  map.resources :projectparts, :has_many => :solutions
  map.resources :projectparts, :has_many => :images
  map.resources :projectparts, :has_many => :documents
  map.resources :solutions, :has_many => :images
  map.resources :solutions, :has_many => :documents
  map.resources :images
  map.resources :documents


  map.connect ':controller/:action/:id'
  map.connect ':controller/:action/:id.:format'

end

1 个答案:

答案 0 :(得分:2)

关于路线生成问题的答案是:shallow => true

提供浅选项将为内部资源的收集方法创建更长的路由,同时为这些嵌套资源的成员方法提供浅层路由。仔细看看创建的路线,你会看到这一点。

基本上需要id(编辑,更新,显示,销毁)的路由将是仅包含最内层资源的短路径。不需要id(index,new)的路由会稍微长一些,包含直接的父路径。

您不需要project_projectpart_path,因为project_projectpart中的项目可以隐含在项目部分中。而只需将projectpart_path用于现有项目部分。

你的第二个例子提供了那些缺失的路由,因为它从不给出浅层选项。

在网址中将'projectpart'更改为'part'就像在其定义中添加:as选项一样简单。抱歉,它不适用于速记:has_many版本

map.resources :projects, :shallow => true do |project|
  project.resources :projectparts, :as => "part" do |part|
    part.resources :solutions do |solution|
      solution.resources :images
      solution.resources :documents
    end
  end
end