As we know that in rails, in order to reach to any page or URL we have to create routes and resources for the same.
Namespaces and scopes are wrappers which can hold our resources inside them and create the routes accordingly.
Now if we talk about namespaces , then basically what a namespace does is organize a group of controllers under it.
For example lets say we have a blog controller which has to be grouped under admin namespace so it would be done like this
namespace :admin do
resources :blogs
end
and the routes created would be like this
![](http://findnerd.s3.amazonaws.com/imagedata/6367/6367.png)
Namespace adds a prefix to the routes with the defined name in it.
As in the above example we gave the namespace as Admin so it added prefix admin to all the routes as we can see.
Now if we want to group all the similar controllers under one wrapper without having to add the prefix to all the routes then we use Scope.
Scopes will allow us to put the controller under Admin but wont prefix the controller#action path with admin.
This can be done like this
scope '/admin' do
resources :blogs
end
and the routes created would be like this
![](http://findnerd.s3.amazonaws.com/imagedata/6368/6368.png)
So in the above screenshot we can see that it did not add the prefix admin to the controller action routes.
This is how namespace and scope works. Hope the blog was helpful.
0 Comment(s)