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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3029954/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:46:50  来源:igfitidea点击:

difference between scope and namespace of ruby-on-rails 3 routing

ruby-on-rails

提问by never_had_a_name

I can't understand what the difference is between a namespace and a scope in the routing of ruby-on-rails 3.

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

Could someone please explain?

有人可以解释一下吗?

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

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

采纳答案by alternative

The difference lies in the paths generated.

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

The paths are admin_posts_pathand admin_comments_pathfor the namespace, while they are just posts_pathand comments_pathfor the scope.

该路径admin_posts_pathadmin_comments_path对命名空间,而他们只是posts_pathcomments_path为范围。

You can get the same result as a namespace by passing the :name_prefixoption to scope.

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

回答by ynkr

examples always help me, so here is an example:

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

namespace :blog do
  resources :contexts
end

will give us the following routes:

将为我们提供以下路线:

    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"}

Using scope...

使用范围...

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

Will give us:

会给我们:

     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"}

Here is some good reading on the subject: http://edgeguides.rubyonrails.org/routing.html#controller-namespaces-and-routing

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

回答by montrealmike

from the rails guide

导轨引导

"The namespace scope will automatically add :asas well as :moduleand :pathprefixes."

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

so

所以

namespace "admin" do
  resources :contexts
end

is the same as

是相同的

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

回答by lakesare

Both scopeand namespaceare scoping a set of routes to the given default options.
Except that there are no default options for scope, and for namespace:path, :as, :module, :shallow_pathand :shallow_prefixoptions all default to the name of the namespace.

无论范围命名空间是观察一组路线到指定默认选项。
不同之处在于有没有为默认选项范围,以及命名空间:path:as:module:shallow_path:shallow_prefix选择所有默认的命名空间的名称。

Available optionsfor both scopeand namespacecorrespond to those of match.

可用选项为两个范围,并命名那些相对应的匹配

回答by V K Singh

scopeis bit complex, but provides more options to fine-tune exactly what you want to do.

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

scopesupports three options: module, path and as. If you see scope with all it options, it will be exactly same as namespace.

scope支持三个选项:module、path 和 as。如果您看到带有所有选项的范围,它将与命名空间完全相同。

In other words, routes generated by

换句话说,由生成的路由

namespace :admin do
  resources :posts
end

is same as

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

In other words, we can say that there are no default options for scopeas compared to namespace. namespaceadd all these options by default. Thus using scope, we can more fine tune the routes as required.

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

If you take a deep look into scopeand namespacedefault behaviour, you will find that scopeby default supports only :pathoption, where as namespacesupports three options module, path and asby default.

如果您深入研究作用域命名空间默认行为,您会发现该作用域默认仅支持:path选项,而 as命名空间默认支持三个选项模块 path 和 as

For more info, please refer a doc namespace-and-routing.

有关更多信息,请参阅 doc namespace-and-routing