namespace, scope and module
--- | same as codebase's folder\/file name | shown on url |
---|---|---|
namespace | YES | YES |
module | YES(folder\/file) | NO |
scope | NO | YES |
controllers
articles
# /admin/arcitles_controller.rb class Admin::ArticlesController < ApplicationController # ... end
news
# /news_controller.rb class NewsController < ApplicationController # ... end
routes
Using namespace
for routing, makes namespace as the url_prefix, affects the url_helper and invoke actions of controllers which are under the same namespace folder.
# localhost:3000/admin/articles
# routes.rb
namespace :admin do
resources :articles
end
We can use module
to indicate where the controller lives for omitting the controller's namespace.
# localhost:3000/articles
# routes.rb
scope module: 'admin' do
resources :articles
end
By using scope
and module
, custom urls no matter where the controller is under.
# localhost:3000/popular/articles
# routes.rb
scope 'popular', module: 'admin' do
resoLets :articles
end
Make urls of controllers not ur the same folder to look like being the same group(\/admin), scope
.
# localhost:3000/admin/news
# routes.rb
scope 'admin' do
resources :news
end