At the routing level, there are two methods that can be used to declare them: and resource. The former is for plural resources, the latter for singular ones.

Declaring a resource means to generate several default routes with just one line of code.

It generates:

Remove Routes

  1. resources :books, only: [:new, :create, :show]
  2. # equivalent to
  3. resources :books, except: [:index, :edit, :update, :destroy]

Add Routes

Alongside with default routes we can specify extra routes for single (member) or multiple (collection) resources.

  1. member do
  2. # Generates /books/1/toggle, maps to Books::Toggle, named :toggle_book
  3. get 'toggle'
  4. end
  5. collection do
  6. get 'search'
  7. end
  8. end

Imagine we have a controller named manuscripts, where we have actions like Manuscripts::Index, but we still want to expose those resources as /books. Using the :controller option will save our day.

  1. resources :books, controller: 'manuscripts'
  2. # GET /books/1 will route to Manuscripts::Show, etc.

It generates:

VerbPathActionNameNamed Route
GET/accountAccount::Show:show:account
GET/account/newAccount::New:new:new_account
POST/accountAccount::Create:create:account
GET/account/editAccount::Edit:edit:edit_account
PATCH/accountAccount::Update:update:account
DELETE/accountAccount::Destroy:destroy:account

Remove Routes

  1. resource :account, only: [:show, :edit, :update, :destroy]
  2. # equivalent to
  3. resource :account, except: [:new, :create]

Add Routes

  1. member do
  2. get 'avatar'
  3. end
  4. collection do
  5. # Generates /account/authorizations, maps to Account::Authorizations, named :authorizations_account
  6. get 'authorizations'
  7. end
  8. end
  1. resource :account, controller: 'customer'

Plural to plural

It generates default routes for books and the following ones.

Plural to singular

  1. resources :books do
  2. resource :cover
  3. end

It generates default routes for books and the following ones.

VerbPathActionNameNamed Route
GET/books/:book_id/coverBooks::Cover::Show:show:book_cover
GET/books/:book_id/cover/newBooks::Cover::New:new:new_book_cover
POST/books/:book_id/coverBooks::Cover::Create:create:book_cover
GET/books/:book_id/cover/editBooks::Cover::Edit:edit:edit_book_cover
PATCH/books/:book_id/coverBooks::Cover::Update:update:book_cover
DELETE/books/:book_id/coverBooks::Cover::Destroy:destroy:book_cover
  1. resource :account do
  2. resources :api_keys
  3. end

It generates default routes for account and the following ones.

Singular To Singular

  1. resource :account do
VerbPathActionNameNamed Route
GET/account/avatarAccount::Avatar::Show:show:account_avatar
GET/account/avatar/newAccount::Avatar::New:new:new_account_avatar
POST/account/avatarAccount::Avatar::Create:create:account_avatar
GET/account/avatar/editAccount::Avatar::Edit:edit:edit_account_avatar
PATCH/account/avatarAccount::Avatar::Update:update:account_avatar
DELETE/account/avatarAccount::Avatar::Destroy:destroy:account_avatar