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

Ruby on-Rails

Ruby on-Rails Problem Overview


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

Could someone please explain?

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

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

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

The difference lies in the paths generated.

The paths are admin_posts_path and admin_comments_path for the namespace, while they are just posts_path and comments_path for the scope.

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

Solution 2 - Ruby on-Rails

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

Solution 3 - Ruby on-Rails

from the rails guide

"The namespace scope will automatically add :as as well as :module and :path prefixes."

so

namespace "admin" do
  resources :contexts
end

is the same as

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

Solution 4 - Ruby on-Rails

Both scope and namespace are 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_path and :shallow_prefix options all default to the name of the namespace.

Available options for both scope and namespace correspond to those of match.

Solution 5 - Ruby on-Rails

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

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

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 scope as compared to namespace. namespace add all these options by default. Thus using scope, we can more fine tune the routes as required.

If you take a deep look into scope and namespace default behaviour, you will find that scope by default supports only :path option, where as namespace supports three options module, path and as by default.

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionnever_had_a_nameView Question on Stackoverflow
Solution 1 - Ruby on-RailsalternativeView Answer on Stackoverflow
Solution 2 - Ruby on-RailsynkrView Answer on Stackoverflow
Solution 3 - Ruby on-RailsmontrealmikeView Answer on Stackoverflow
Solution 4 - Ruby on-RailslakesareView Answer on Stackoverflow
Solution 5 - Ruby on-RailsV K SinghView Answer on Stackoverflow