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

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


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.


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