代码之家  ›  专栏  ›  技术社区  ›  steven_noble

浅嵌套轨道布线:AS

  •  2
  • steven_noble  · 技术社区  · 15 年前

    我想要:

    • 属于的每个项目部件 项目。

    • 每个解决方案都属于 ProjectArt(通过 那个项目部分)。

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

    • 每个文档都属于 解决方案(以及项目和 通过该解决方案进行项目部分。)

    • 每个URL都要像 可能的。

    • 出现“ProjectPart”的每一个案例 作为每个URL的“部分”。(我不能) 把Heroku上的模型称为“零件”。

    有人能告诉我为什么…

    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
    

    …在我的URL中留下了一大堆“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 回复  |  直到 15 年前
        1
  •  2
  •   EmFi    15 年前

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

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

    基本上,需要ID(编辑、更新、显示、销毁)的路由将是只包含最内部资源的短路由。不需要ID(index,new)的路由将稍长一些,包含直接父路由。

    你不需要 project_projectpart_path 因为项目在 project_projectpart 可以从项目部分暗示。只需使用 projectpart_path 对于现有项目部件。

    第二个例子提供了那些丢失的路径,因为它从不提供浅层选项。

    在URL中将“projectpart”更改为“part”与在其定义中添加:as选项一样简单。对不起,它不能用速记:有很多版本

    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