Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Difference between scope and namespace in Rails

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 482
    Comment on it

    As we know,Both scope and namespace have to do something with the config/routes.rb file  .Yes,in some way ,both are wrapper for our well-known resources method. First,understand the working of scope by examining how it generates paths.Consider this:

    scope 'alpha', module: 'bar', as: 'benz' do
      resources :posts
    end
      Prefix Verb   URI Pattern                   Controller#Action
        benz_posts GET    /alpha/posts(.:format)          bar/posts#index
                  POST   /alpha/posts(.:format)          bar/posts#create
     new_benz_post GET    /alpha/posts/new(.:format)      bar/posts#new
    edit_benz_post GET    /alpha/posts/:id/edit(.:format) bar/posts#edit
         benz_post GET    /alpha/posts/:id(.:format)      bar/posts#show
                  PATCH  /alpha/posts/:id(.:format)      bar/posts#update
                  PUT    /alpha/posts/:id(.:format)      bar/posts#update
                  DELETE /alpha/posts/:id(.:format)      bar/posts#destroy

    Now ,Let's analyze the paths being generated by namespace ,if we write something like this :

    namespace :alpha do
      resources :posts
    end

    doing rake routes gives us:

         Prefix Verb   URI Pattern                   Controller#Action
        alpha_posts GET    /alpha/posts(.:format)          alpha/posts#index
                  POST   /alpha/posts(.:format)          alpha/posts#create
     new_alpha_post GET    /alpha/posts/new(.:format)      alpha/posts#new
    edit_alpha_post GET    /alpha/posts/:id/edit(.:format) alpha/posts#edit
         alpha_post GET    /alpha/posts/:id(.:format)      alpha/posts#show
                  PATCH  /alpha/posts/:id(.:format)      alpha/posts#update
                  PUT    /alpha/posts/:id(.:format)      alpha/posts#update
                  DELETE /alpha/posts/:id(.:format)      alpha/posts#destroy

    One thing to note ,When we are working with namespace ,  first create a folder like

    app/controllers/alpha and have a structure like this :

    module Alpha
      class PostsController < ApplicationController
      
      end
    end

    Now ,amid all this ,you may ask a very pertinent question "Why do we need them?".

    You may wish to organize groups of controllers under a namespace. For example , you might group a number of administrative controllers under an Admin:: namespace .So

    namespace is like a wrapper that wraps controllers whom we want to be grouped together.

    Thanks for reading.

 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: