代码之家  ›  专栏  ›  技术社区  ›  Christoph Schiessl Joeyjoejoejr

带有路径前缀的浅路由?

  •  4
  • Christoph Schiessl Joeyjoejoejr  · 技术社区  · 15 年前

    最近,我在一个应用程序中更改了一些嵌套资源,以使用浅路由。它工作得很好,我能够简化我的视图和控制器。

    map.with_options :path_prefix => "blog" do |blog|
      blog.resources :posts do |posts|
        posts.resources :comments
      end
    end
    

    请注意,所有路由都按预期以“/blog”作为前缀。

    # $ rake routes
    #             posts GET    /blog/posts(.:format)                            {:controller=>"posts", :action=>"index"}
    #                   POST   /blog/posts(.:format)                            {:controller=>"posts", :action=>"create"}
    #          new_post GET    /blog/posts/new(.:format)                        {:controller=>"posts", :action=>"new"}
    #         edit_post GET    /blog/posts/:id/edit(.:format)                   {:controller=>"posts", :action=>"edit"}
    #              post GET    /blog/posts/:id(.:format)                        {:controller=>"posts", :action=>"show"}
    #                   PUT    /blog/posts/:id(.:format)                        {:controller=>"posts", :action=>"update"}
    #                   DELETE /blog/posts/:id(.:format)                        {:controller=>"posts", :action=>"destroy"}
    #     post_comments GET    /blog/posts/:post_id/comments(.:format)          {:controller=>"comments", :action=>"index"}
    #                   POST   /blog/posts/:post_id/comments(.:format)          {:controller=>"comments", :action=>"create"}
    #  new_post_comment GET    /blog/posts/:post_id/comments/new(.:format)      {:controller=>"comments", :action=>"new"}
    # edit_post_comment GET    /blog/posts/:post_id/comments/:id/edit(.:format) {:controller=>"comments", :action=>"edit"}
    #      post_comment GET    /blog/posts/:post_id/comments/:id(.:format)      {:controller=>"comments", :action=>"show"}
    #                   PUT    /blog/posts/:post_id/comments/:id(.:format)      {:controller=>"comments", :action=>"update"}
    #                   DELETE /blog/posts/:post_id/comments/:id(.:format)      {:controller=>"comments", :action=>"destroy"}
    

    新的路由配置如下所示:

    map.with_options :path_prefix => "blog", :shallow => true do |blog|
      blog.resources :posts do |posts|
        posts.resources :comments
      end
    end
    

    现在,我的一些路线中缺少“/blog”前缀。

    # $ rake routes
    #            posts GET    /blog/posts(.:format)                  {:controller=>"posts", :action=>"index"}
    #                  POST   /blog/posts(.:format)                  {:controller=>"posts", :action=>"create"}
    #         new_post GET    /blog/posts/new(.:format)              {:controller=>"posts", :action=>"new"}
    #        edit_post GET    /posts/:id/edit(.:format)              {:controller=>"posts", :action=>"edit"}
    #             post GET    /posts/:id(.:format)                   {:controller=>"posts", :action=>"show"}
    #                  PUT    /posts/:id(.:format)                   {:controller=>"posts", :action=>"update"}
    #                  DELETE /posts/:id(.:format)                   {:controller=>"posts", :action=>"destroy"}
    #    post_comments GET    /posts/:post_id/comments(.:format)     {:controller=>"comments", :action=>"index"}
    #                  POST   /posts/:post_id/comments(.:format)     {:controller=>"comments", :action=>"create"}
    # new_post_comment GET    /posts/:post_id/comments/new(.:format) {:controller=>"comments", :action=>"new"}
    #     edit_comment GET    /comments/:id/edit(.:format)           {:controller=>"comments", :action=>"edit"}
    #          comment GET    /comments/:id(.:format)                {:controller=>"comments", :action=>"show"}
    #                  PUT    /comments/:id(.:format)                {:controller=>"comments", :action=>"update"}
    #                  DELETE /comments/:id(.:format)                {:controller=>"comments", :action=>"destroy"}
    

    我正在寻找一个解决方案,使前缀回到所有路线。我知道它正在使用名称空间( map.namespace :blog do

    2 回复  |  直到 15 年前
        1
  •  6
  •   Adam Alexander    15 年前

    文件似乎表明,这种确切的行为是出于设计:

    (来自 http://api.rubyonrails.org/classes/ActionController/Resources.html#M000501 )

    map.with_options :path_prefix => "blog" do |blog|
      blog.resources :posts do |posts|
        posts.resources :comments, :only => [:index, :create, :new]
      end
      blog.resources :comments, :except => [:index, :create, :new]
    end
    

    #             posts GET    /blog/posts                               {:controller=>"posts", :action=>"index"}
    #                   POST   /blog/posts                               {:controller=>"posts", :action=>"create"}
    #          new_post GET    /blog/posts/new                           {:controller=>"posts", :action=>"new"}
    #         edit_post GET    /blog/posts/:id/edit                      {:controller=>"posts", :action=>"edit"}
    #              post GET    /blog/posts/:id                           {:controller=>"posts", :action=>"show"}
    #                   PUT    /blog/posts/:id                           {:controller=>"posts", :action=>"update"}
    #                   DELETE /blog/posts/:id                           {:controller=>"posts", :action=>"destroy"}
    #     post_comments GET    /blog/posts/:post_id/comments             {:controller=>"comments", :action=>"index"}
    #                   POST   /blog/posts/:post_id/comments             {:controller=>"comments", :action=>"create"}
    #  new_post_comment GET    /blog/posts/:post_id/comments/new         {:controller=>"comments", :action=>"new"}
    #      edit_comment GET    /blog/comments/:id/edit                   {:controller=>"comments", :action=>"edit"}
    #           comment GET    /blog/comments/:id                        {:controller=>"comments", :action=>"show"}
    #                   PUT    /blog/comments/:id                        {:controller=>"comments", :action=>"update"}
    #                   DELETE /blog/comments/:id                        {:controller=>"comments", :action=>"destroy"}
    

    希望这有帮助!

        2
  •  5
  •   Raimonds Simanovskis    15 年前

    最简单的解决方案可能不是使用:shallow选项,而是使用其他资源定义创建相同的路由:

    map.with_options :path_prefix => "blog" do |blog|
      blog.resources :posts do |posts|
        posts.resources :comments, :only => [:index, :create, :new]
      end
    end
    map.resources :comments, :path_prefix => "blog",
                  :except => [:index, :create, :new]
    

    其中给出了以下管线定义:

    # $ rake routes
    #            posts GET    /blog/posts(.:format)                       {:action=>"index", :controller=>"posts"}
    #                  POST   /blog/posts(.:format)                       {:action=>"create", :controller=>"posts"}
    #         new_post GET    /blog/posts/new(.:format)                   {:action=>"new", :controller=>"posts"}
    #        edit_post GET    /blog/posts/:id/edit(.:format)              {:action=>"edit", :controller=>"posts"}
    #             post GET    /blog/posts/:id(.:format)                   {:action=>"show", :controller=>"posts"}
    #                  PUT    /blog/posts/:id(.:format)                   {:action=>"update", :controller=>"posts"}
    #                  DELETE /blog/posts/:id(.:format)                   {:action=>"destroy", :controller=>"posts"}
    #    post_comments GET    /blog/posts/:post_id/comments(.:format)     {:action=>"index", :controller=>"comments"}
    #                  POST   /blog/posts/:post_id/comments(.:format)     {:action=>"create", :controller=>"comments"}
    # new_post_comment GET    /blog/posts/:post_id/comments/new(.:format) {:action=>"new", :controller=>"comments"}
    #     edit_comment GET    /blog/comments/:id/edit(.:format)           {:action=>"edit", :controller=>"comments"}
    #          comment GET    /blog/comments/:id(.:format)                {:action=>"show", :controller=>"comments"}
    #                  PUT    /blog/comments/:id(.:format)                {:action=>"update", :controller=>"comments"}
    #                  DELETE /blog/comments/:id(.:format)                {:action=>"destroy", :controller=>"comments"}