0
0
RailsHow-ToBeginner · 4 min read

How to Use Namespace in Routes in Ruby on Rails

In Ruby on Rails, use namespace in your config/routes.rb file to group related routes under a common URL prefix and controller module. This helps organize controllers and URLs, for example, namespace :admin do creates routes like /admin/products handled by Admin::ProductsController.
📐

Syntax

The namespace method wraps routes inside a URL path prefix and a controller module. The syntax is:

  • namespace :name do ... end: groups routes under /name URL prefix.
  • Controllers must be inside a module named after the namespace, e.g., Name::ControllerName.
  • Routes inside the block inherit the namespace path and controller scope.
ruby
Rails.application.routes.draw do
  namespace :admin do
    resources :products
  end
end
💻

Example

This example shows how to create an admin namespace for product management. It generates URLs prefixed with /admin and expects controllers inside the Admin module.

ruby
Rails.application.routes.draw do
  namespace :admin do
    resources :products
  end
end

# Controller: app/controllers/admin/products_controller.rb
module Admin
  class ProductsController < ApplicationController
    def index
      render plain: "Admin Products Index"
    end
  end
end
Output
Visiting /admin/products shows "Admin Products Index"
⚠️

Common Pitfalls

Common mistakes include:

  • Not placing controllers inside the correct module folder (e.g., app/controllers/admin/ for Admin namespace).
  • Using scope instead of namespace when you want module scoping.
  • Forgetting that namespace adds both URL prefix and controller module, so controller names must match.
ruby
Rails.application.routes.draw do
  # Wrong: controller not in Admin module
  namespace :admin do
    get 'dashboard', to: 'dashboard#index'
  end
end

# Controller placed at app/controllers/dashboard_controller.rb (WRONG)
class DashboardController < ApplicationController
  def index
    render plain: "Dashboard"
  end
end

# Correct controller placement:
module Admin
  class DashboardController < ApplicationController
    def index
      render plain: "Admin Dashboard"
    end
  end
end
📊

Quick Reference

ConceptDescriptionExample
namespaceGroups routes under URL prefix and controller modulenamespace :admin do ... end
URL prefixAdded automatically from namespace name/admin/products
Controller moduleControllers must be inside matching moduleAdmin::ProductsController
resourcesDefines RESTful routes inside namespaceresources :products
scope vs namespacescope only adds URL prefix, namespace adds module tooscope :admin do ... end vs namespace :admin do ... end

Key Takeaways

Use namespace in routes to group URLs and controllers under a common module and path.
Controllers must be placed inside a folder and module matching the namespace name.
Namespace adds both URL prefix and controller module scope automatically.
Common errors include mismatched controller modules or using scope when namespace is needed.
Use resources inside namespaces to create RESTful routes with proper URL and controller structure.