Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Namespaces And Scopes in Rails

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 2
    • 0
    • 788
    Comment on it

    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.

    • Namespaces

    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

    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.

    • Scope

    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

    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.

    Namespaces And Scopes in Rails

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: