首页 文章

ruby-on-rails 3路由的范围和命名空间之间的差异

提问于
浏览
103

我无法理解ruby-on-rails 3路由中命名空间和范围之间的区别 .

有人可以解释一下吗?

namespace "admin" do
  resources :posts, :comments
end

scope :module => "admin" do
  resources :posts, :comments
end

5 回答

  • 63

    不同之处在于生成的路径 .

    命名空间的路径为 admin_posts_pathadmin_comments_path ,而范围只有 posts_pathcomments_path .

    通过将 :name_prefix 选项传递给作用域,可以获得与命名空间相同的结果 .

  • 97

    示例总是帮助我,所以这是一个例子:

    namespace :blog do
      resources :contexts
    end
    

    会给我们以下路线:

    blog_contexts GET    /blog/contexts(.:format)          {:action=>"index", :controller=>"blog/contexts"}
                      POST   /blog/contexts(.:format)          {:action=>"create", :controller=>"blog/contexts"}
     new_blog_context GET    /blog/contexts/new(.:format)      {:action=>"new", :controller=>"blog/contexts"}
    edit_blog_context GET    /blog/contexts/:id/edit(.:format) {:action=>"edit", :controller=>"blog/contexts"}
         blog_context GET    /blog/contexts/:id(.:format)      {:action=>"show", :controller=>"blog/contexts"}
                      PUT    /blog/contexts/:id(.:format)      {:action=>"update", :controller=>"blog/contexts"}
                      DELETE /blog/contexts/:id(.:format)      {:action=>"destroy", :controller=>"blog/contexts"}
    

    使用范围......

    scope :module => 'blog' do
      resources :contexts
    end
    

    会给我们:

    contexts GET    /contexts(.:format)           {:action=>"index", :controller=>"blog/contexts"}
                  POST   /contexts(.:format)           {:action=>"create", :controller=>"blog/contexts"}
      new_context GET    /contexts/new(.:format)       {:action=>"new", :controller=>"blog/contexts"}
     edit_context GET    /contexts/:id/edit(.:format)  {:action=>"edit", :controller=>"blog/contexts"}
          context GET    /contexts/:id(.:format)       {:action=>"show", :controller=>"blog/contexts"}
                  PUT    /contexts/:id(.:format)       {:action=>"update", :controller=>"blog/contexts"}
                  DELETE /contexts/:id(.:format)       {:action=>"destroy", :controller=>"blog/contexts"}
    

    以下是关于这个主题的一些好读物:http://edgeguides.rubyonrails.org/routing.html#controller-namespaces-and-routing

  • 2

    来自rails guide

    “命名空间范围将自动添加 :as 以及 :module:path 前缀 . ”

    所以

    namespace "admin" do
      resources :contexts
    end
    

    是相同的

    scope "/admin", as: "admin", module: "admin" do
      resources :contexts
    end
    
  • 50

    scopenamespace 都在确定给定默认选项的一组路由 .
    除了 scope 没有默认选项,并且 namespace :path:as:module:shallow_path:shallow_prefix 选项都默认为命名空间的名称 .

    Available options scopenamespace 对应于 matchAvailable options .

  • 0

    scope 有点复杂,但提供了更多选项来精确调整您想要做的事情 .

    scope 支持三个 options: module, path and as . 如果您看到包含所有选项的范围,它将与命名空间完全相同 .

    换句话说,生成的路由

    namespace :admin do
      resources :posts
    end
    

    和...一样

    scope module: 'admin', path: 'admin', as: 'admin' do
      resources :posts
    end
    

    换句话说,我们可以说 scope 与命名空间相比没有默认选项 . namespace 默认添加所有这些选项 . 因此,使用范围,我们可以根据需要更精细地调整路线 .

    如果您深入研究 scopenamespace 默认行为,您会发现默认情况下 scope 仅支持 :path 选项,其中 namespace 默认支持三个选项 module, path and as .

    有关详细信息,请参阅doc namespace-and-routing .

相关问题