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
resources :books, only: [:new, :create, :show]
# equivalent to
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.
member do
# Generates /books/1/toggle, maps to Books::Toggle, named :toggle_book
get 'toggle'
end
collection do
get 'search'
end
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.
resources :books, controller: 'manuscripts'
# GET /books/1 will route to Manuscripts::Show, etc.
It generates:
Verb | Path | Action | Name | Named Route |
---|---|---|---|---|
GET | /account | Account::Show | :show | :account |
GET | /account/new | Account::New | :new | :new_account |
POST | /account | Account::Create | :create | :account |
GET | /account/edit | Account::Edit | :edit | :edit_account |
PATCH | /account | Account::Update | :update | :account |
DELETE | /account | Account::Destroy | :destroy | :account |
Remove Routes
resource :account, only: [:show, :edit, :update, :destroy]
# equivalent to
resource :account, except: [:new, :create]
Add Routes
member do
get 'avatar'
end
collection do
# Generates /account/authorizations, maps to Account::Authorizations, named :authorizations_account
get 'authorizations'
end
end
resource :account, controller: 'customer'
Plural to plural
It generates default routes for books and the following ones.
Plural to singular
resources :books do
resource :cover
end
It generates default routes for books and the following ones.
Verb | Path | Action | Name | Named Route |
---|---|---|---|---|
GET | /books/:book_id/cover | Books::Cover::Show | :show | :book_cover |
GET | /books/:book_id/cover/new | Books::Cover::New | :new | :new_book_cover |
POST | /books/:book_id/cover | Books::Cover::Create | :create | :book_cover |
GET | /books/:book_id/cover/edit | Books::Cover::Edit | :edit | :edit_book_cover |
PATCH | /books/:book_id/cover | Books::Cover::Update | :update | :book_cover |
DELETE | /books/:book_id/cover | Books::Cover::Destroy | :destroy | :book_cover |
resource :account do
resources :api_keys
end
It generates default routes for account and the following ones.
Singular To Singular
resource :account do
Verb | Path | Action | Name | Named Route |
---|---|---|---|---|
GET | /account/avatar | Account::Avatar::Show | :show | :account_avatar |
GET | /account/avatar/new | Account::Avatar::New | :new | :new_account_avatar |
POST | /account/avatar | Account::Avatar::Create | :create | :account_avatar |
GET | /account/avatar/edit | Account::Avatar::Edit | :edit | :edit_account_avatar |
PATCH | /account/avatar | Account::Avatar::Update | :update | :account_avatar |
DELETE | /account/avatar | Account::Avatar::Destroy | :destroy | :account_avatar |