Prev | Current Page 302 | Next

Brad Ediger

"Advanced Rails"


Now that we have decided on a set of URLs for the application, we can start writing
code. First, we have identified the types of resources that will be involved. They were
implied previously, but we will describe them:
Cart factory (/carts)
This resource is responsible for generating carts. An empty POST to /carts will
create a new cart and return its URI in the HTTP Location header.
Cart (/carts/4)
Represents a cart; in this model, shopping cart state is kept explicitly in this
resource rather than in the user??™s session.
Line Item (/carts/4/products/123)
Represents an instance of a product in a user??™s cart. Subordinate to (nested
within) the cart resource.
These three types of resources are just that??”types, not resources
themselves. Although there is only one ???cart??? resource type, there are
potentially infinitely many ???cart??? resources (/carts/4, /carts/5, and
so on). This is an important distinction to keep in mind. Typically,
resource types will either have cardinality one (such as our ???cart factory???
type) or infinity (as with our ???carts??? type).
In Rails, the first two types are handled by the same controller, by convention. Operations
on the collection as a whole (such as POST /carts) and on its members (such as
Action Non-RESTful URI (POST method) RESTful method and URI
Create cart (N/A) POST /carts
Add item /carts/add_product/123 PUT /carts/4/products/123
Update quantity /carts/update_quantity/
123?quantity=2
PUT /carts/4/products/123
line_item[quantity]=2
Remove item /carts/remove_item/123 DELETE /carts/4/products/123
Empty cart /carts/empty_cart DELETE /carts/4
What Is REST? | 201
DELETE /carts/4) are traditionally routed through the same controller.


Pages:
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314