Handy Rails Tips

Define meta-resources for non-CRUD actions

Because many Rails developers believe they are constrained to 7 RESTful (CRUD) actions, they struggle to understand where they should define actions that fall outside of these defaults. For example, POST users/1/archive.

One solution that has become popular, is to define a meta-resource or pseudo-resource to represent these actions as a new resource. So POST users/1/archvie becomes POST users/1/archivals.

The distinction here may be subtle, but it’s an important one. Instead of sending a request to change state for a resource called user/1, we are sending a request to create a new resource called an archival for user/1.

Conceptually speaking, this doesn’t match the business logic we are attempting to model (archiving a User).

Actions with non-default names should be defined on the resource that they are modifying.