Scoping routes

suggest change

Rails provides several ways to organize your routes.

Scope by URL:

scope 'admin' do
  get 'dashboard', to: 'administration#dashboard'
  resources 'employees'
end

This generates the following routes:

get       '/admin/dashboard',          to: 'administration#dashboard'
get       '/admin/employees',          to: 'employees#index'
post      '/admin/employees',          to: 'employees#create'
get       '/admin/employees/new',      to: 'employees#new'
get       '/admin/employees/:id/edit', to: 'employees#edit'
get       '/admin/employees/:id',      to: 'employees#show'
patch/put '/admin/employees/:id',      to: 'employees#update'
delete    '/admin/employees/:id',      to: 'employees#destroy'

It may make more sense, on the server side, to keep some views in a different subfolder, to separate admin views from user views.

Scope by module

scope module: :admin do
  get 'dashboard', to: 'administration#dashboard'
end

module looks for the controller files under the subfolder of the given name

get       '/dashboard',          to: 'admin/administration#dashboard'

You can rename the path helpers prefix by adding an as parameter

scope 'admin', as: :administration do
  get 'dashboard' # dashboard#dashboard
end

# => administration_dashboard_path

Rails provides a convenient way to do all the above, using the namespace method. The following declarations are equivalent

namespace :admin do
end

scope 'admin', module: :admin, as: :admin do
end

Scope by controller

scope controller: :management do
  get 'dashboard'
  get 'performance'
end

This generates these routes:

get       '/dashboard',          to: 'management#dashboard'
get       '/performance',        to: 'management#performance'

Shallow Nesting

Resource routes accept a :shallow option that helps to shorten URLs where possible. Resources shouldn’t be nested more than one level deep. One way to avoid this is by creating shallow routes. The goal is to leave off parent collection URL segments where they are not needed. The end result is that the only nested routes generated are for the :index, :create, and :new actions. The rest are kept in their own shallow URL context. Two scope options customize shallow routes:

scope shallow_path: "sekret" do
  resources :articles do
    resources :comments, shallow: true
  end
end
scope shallow_prefix: "sekret" do
  resources :articles do
    resources :comments, shallow: true
  end
end

We can also illustrate shallow routes more by:

resources :auctions, shallow: true do
  resources :bids do
    resources :comments
  end
end

Alternatively:

resources :auctions do
  shallow do
    resources :bids do
      resources :comments
    end
  end
end

The resulting routes are:

bid_comments    GET    /bids/:bid_id/comments(.:format)         comments#index
                POST   /bids/:bid_id/comments(.:format)         comments#create
new_bid_comment GET    /bids/:bid_id/comments/new(.:format)     comments#new
edit_comment    GET    /comments/:id/edit(.:format)             comments#edit
comment         GET    /comments/:id(.:format)                  comments#show
                PATCH  /comments/:id(.:format)                  comments#update
                PUT    /comments/:id(.:format)                  comments#update
                DELETE /comments/:id(.:format)                  comments#destroy
auction_bids    GET    /auctions/:auction_id/bids(.:format)     bids#index
                POST   /auctions/:auction_id/bids(.:format)     bids#create
new_auction_bid GET    /auctions/:auction_id/bids/new(.:format) bids#new
edit_bid        GET    /bids/:id/edit(.:format)                 bids#edit
bid             GET    /bids/:id(.:format)                      bids#show
                PATCH  /bids/:id(.:format)                      bids#update
                PUT    /bids/:id(.:format)                      bids#update
                DELETE /bids/:id(.:format)                      bids#destroy
auctions        GET    /auctions(.:format)                      auctions#index
                POST   /auctions(.:format)                      auctions#create
new_auction     GET    /auctions/new(.:format)                  auctions#new
edit_auction    GET    /auctions/:id/edit(.:format)             auctions#edit
auction         GET    /auctions/:id(.:format)                  auctions#show
                PATCH  /auctions/:id(.:format)                  auctions#update
                PUT    /auctions/:id(.:format)                  auctions#update
                DELETE /auctions/:id(.:format)                  auctions#destroy

If you analyze the routes generated carefully, you’ll notice that the nested parts of the URL are only included when they are needed to determine what data to display.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents