Collection vs. Member Actions
Rails is crucial for setting up clean, intuitive, and RESTful routes for your application. Let’s break it down to ensure clarity on collection and member actions, as well as the appropriate usage of resources
and resource
.
Collection Actions
- Operate on the entire collection of resources.
- Do not require an individual resource ID.
Common collection actions include:
index
: Displays a list of all resources.create
: Adds a new resource to the collection.new
: Provides a form to create a new resource.
Example:
GET /users # index
POST /users # create
GET /users/new # new
Member Actions
- Operate on a single resource within the collection.
- Require an individual resource ID.
Common member actions include:
show
: Displays a specific resource.edit
: Provides a form to edit a specific resource.update
: Updates a specific resource.destroy
: Deletes a specific resource.
Example:
GET /users/:id # show
GET /users/:id/edit # edit
PATCH/PUT /users/:id # update
DELETE /users/:id # destroy
Using resources
The resources
method is used for managing a collection of resources. Here's an example using resources :users
:
Rails.application.routes.draw do
resources :users do
collection do
get 'active' # Custom collection route
end
member do
get 'profile' # Custom member route
end
end
end
This generates the following routes:
Collection Routes:
GET /users # index
GET /users/new # new
POST /users # create
GET /users/active # Custom collection action
Member Routes:
GET /users/:id # show
GET /users/:id/edit # edit
PATCH /users/:id # update
DELETE /users/:id # destroy
GET /users/:id/profile # Custom member action
Using resource
The resource
method is used for managing a single resource. Here's an example using resource :profile
:
Rails.application.routes.draw do
resource :profile do
collection do
get 'settings' # Custom collection route for the singular resource
end
end
end
This generates the following routes:
Collection Routes:
GET /profile/settings # Custom collection action for the singular resource
Member Routes:
GET /profile # show
GET /profile/new # new
POST /profile # create
GET /profile/edit # edit
PATCH /profile # update
DELETE /profile # destroy
Custom Routes for Collections and Members
You can add custom routes for both collections and members using the collection
and member
blocks within the resources
or resource
methods.
Example with resources
:
Rails.application.routes.draw do
resources :articles do
collection do
get 'published' # Custom collection action
end
member do
get 'author' # Custom member action
end
end
end
This generates:
GET /articles/published # Custom collection action
GET /articles/:id/author # Custom member action
Example with resource
:
Rails.application.routes.draw do
resource :dashboard do
collection do
get 'stats' # Custom collection action for singular resource
end
end
end
This generates:
GET /dashboard/stats # Custom collection action for the singular resource
Conclusion
Understanding how to use resources
and resource
in Rails allows you to set up routes that align with RESTful principles. By leveraging collection and member actions appropriately, you can ensure that your application's routing is intuitive and maintains a clear structure. This distinction not only aids in navigation and usability but also supports maintainable and scalable code architecture.
Feel free to reach out if you have more questions about Rails routing or any other topic!