Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

People also ask

What is scope in routes in Rails?

Similar to the :namespace, the :scope has few options which can control the changes in URL path, controller structure and Prefix path. Scope provides few options of :path , :as and :module . Generally the scope can be given as follows, config/routes.rbRails.application.routes.draw do.

What is namespace in Ruby on Rails?

A namespace is a container for multiple items which includes classes, constants, other modules, and more. It is ensured in a namespace that all the objects have unique names for easy identification.

What is namespace route?

This is the simple option. When you use namespace , it will prefix the URL path for the specified resources, and try to locate the controller under a module named in the same manner as the namespace. With the following code in the routes.rb file, namespace :admin do resources :users end.

What is scope route?

Route scopes are a generic mechanism to express this restriction: the new route's nexthop needs to be reachable through an existing route with a lower scope. In other words, you must go through a local host (link scope) before you can reach a remote host (global scope).)


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.


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


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

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.


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.