Rails 路由中 Scope 和 Namespace 的区别

/

Scope

scope 'url_path_prefix', module: 'module_prefix', as: 'named_route_prefix' do
  resources :posts
end

举例:

scope 'foo', module: 'bar', as: 'baz' do
  resources :posts
end
$ rake routes
       Prefix Verb   URI Pattern                   Controller#Action
    baz_posts GET    /foo/posts(.:format)          bar/posts#index
              POST   /foo/posts(.:format)          bar/posts#create
 new_baz_post GET    /foo/posts/new(.:format)      bar/posts#new
edit_baz_post GET    /foo/posts/:id/edit(.:format) bar/posts#edit
     baz_post GET    /foo/posts/:id(.:format)      bar/posts#show
              PATCH  /foo/posts/:id(.:format)      bar/posts#update
              PUT    /foo/posts/:id(.:format)      bar/posts#update
              DELETE /foo/posts/:id(.:format)      bar/posts#destroy

Namespace

namespace :foo do
  resources :posts
end
$ rake routes
       Prefix Verb   URI Pattern                   Controller#Action
    foo_posts GET    /foo/posts(.:format)          foo/posts#index
              POST   /foo/posts(.:format)          foo/posts#create
 new_foo_post GET    /foo/posts/new(.:format)      foo/posts#new
edit_foo_post GET    /foo/posts/:id/edit(.:format) foo/posts#edit
     foo_post GET    /foo/posts/:id(.:format)      foo/posts#show
              PATCH  /foo/posts/:id(.:format)      foo/posts#update
              PUT    /foo/posts/:id(.:format)      foo/posts#update
              DELETE /foo/posts/:id(.:format)      foo/posts#destroy

来源:Scoping Rails Routes

Comments