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/nameURL 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
endExample
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
endOutput
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/forAdminnamespace). - Using
scopeinstead ofnamespacewhen you want module scoping. - Forgetting that
namespaceadds 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
endQuick Reference
| Concept | Description | Example |
|---|---|---|
| namespace | Groups routes under URL prefix and controller module | namespace :admin do ... end |
| URL prefix | Added automatically from namespace name | /admin/products |
| Controller module | Controllers must be inside matching module | Admin::ProductsController |
| resources | Defines RESTful routes inside namespace | resources :products |
| scope vs namespace | scope only adds URL prefix, namespace adds module too | scope :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.