A Bit More on Dispatching Requests
Learn about REST requests in detail.
We'll cover the following...
Adding additional actions
Rails resources provide us with an initial set of actions, but we needn’t stop there. In Atom Feeds, we added an interface to allow people to fetch a list of who bought any given product. To do that with Rails, we use an extension to the resources call:
Depot::Application.routes.draw doresources :products doget :who_bought, on: :memberendend
That syntax is straightforward. It says “We want to add a new action named who_bought in line 3, invoked via an HTTP GET in line 3. It applies to each member of the collection of products.””
If we instead specified :collection instead of specifying :member in line 3, the route would apply to the collection as a whole. This is often used for scoping. For example, we may have a collection of products on clearance or products that have been discontinued.
Nested resources
Often, our resources themselves contain additional collections of resources. We may want to allow folks to review our products, for example. Each review would be a resource, and collections of reviews would be associated with each product resource. ...