【问题标题】:difference between scope and namespace of ruby-on-rails 3 routingruby-on-rails 3路由的范围和命名空间之间的区别
【发布时间】:2010-06-12 20:12:46
【问题描述】:

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

谁能解释一下?

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

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

【问题讨论】:

    标签: ruby-on-rails


    【解决方案1】:

    区别在于生成的路径。

    命名空间的路径是admin_posts_pathadmin_comments_path,而作用域的路径只有posts_pathcomments_path

    您可以通过将:name_prefix 选项传递给范围来获得与命名空间相同的结果。

    【讨论】:

    • 路径你的意思是助手的名字对吗?我不了解示波器的功能。如果没有任何变化,它 (:module => "admin") 会做什么?
    • 它将路由路径使用的实际路径更改为“/admin/whatever”,就像命名空间一样。唯一不同的是添加到辅助方法的前缀。
    • 为了更好地理解差异:考虑使用范围通过 URL 进行本地化,使用命名空间进行嵌套,例如 url:domain.com/nl/admin/panel。 nl 是作用域,admin 是命名空间。
    • 我通过:as 而不是:name_prefixscope 也得到相同的结果,如下所示:scope module: "admin", as: "admin",但我不确定其他效果。
    【解决方案2】:

    例子总是对我有帮助,所以这里有一个例子:

    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

    【讨论】:

    • 所以如果你没有在这里使用作用域并且只有:resources:contexts,控制器就不会嵌套在博客:blog/contexts
    【解决方案3】:

    来自rails guide

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

    所以

    namespace "admin" do
      resources :contexts
    end
    

    相同
    scope "/admin", as: "admin", module: "admin" do
      resources :contexts
    end
    

    【讨论】:

      【解决方案4】:

      scopenamespace 都将一组路由限定为给定的默认选项。
      除了 scopenamespace 没有默认选项 :path:as:module:shallow_path:shallow_prefix 选项都默认为命名空间的名称。

      Available options 对于 scopenamespace 都对应于 ma​​tch

      【讨论】:

        【解决方案5】:

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

        范围支持三个选项:模块、路径和as。如果您看到所有 it 选项的范围,它将与命名空间完全相同。

        换句话说,路由生成的

        namespace :admin do
          resources :posts
        end
        

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

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

        如果你深入研究 scopenamespace 的默认行为,你会发现 scope 默认只支持 : path 选项,其中 as namespace 默认支持 module、path 和 as 三个选项。

        更多信息,请参考文档namespace-and-routing

        【讨论】:

        • 如果您出于任何原因尝试添加必需的参数,则范围是最佳解决方案。
        猜你喜欢
        • 1970-01-01
        • 2013-10-09
        • 1970-01-01
        • 1970-01-01
        • 2011-11-07
        • 2012-07-05
        • 1970-01-01
        • 1970-01-01
        • 2012-05-10
        相关资源
        最近更新 更多