Prev | Current Page 307 | Next

Brad Ediger

"Advanced Rails"

line_items.find(params[:id])
end
end
204 | Chapter 7: REST, Resources, and Web Services
The preceding update method illustrates a difference between REST in
theory and RESTful Rails in practice. In Rails, the PUT method is
always mapped to update, while REST in general can also use PUT to
create resources. We work around this by allowing creation from the
update method, to allow us to use the more RESTful design.
Traditionally, Rails would use create for this action, which would
require POSTing ???product_id=123??? to the parent resource at /carts/4/
products. This would be a RESTful design if the server chose the
resulting URI (for example, if the LineItem??™s ID was used instead of a
combination of cart and product ID). However, because the client has
all of the information about the resource??™s URI (/carts/4/products/
123), it should just PUT the resource to that URI.
At first glance, this code may seem to be much more complicated than the original,
non-RESTful code. However, we have moved a great deal of functionality out of the
framework (removing the dependency on sessions) and made the session state into
an explicit set of resources. One of the big advantages that we gain is that the state
now has all of the benefits of REST; state items are addressable, and we can now use
a stateless server, assisting in scalability. This RESTful architecture is much more
uniform than the old architecture, because it uses standard HTTPand CRUD methods
instead of custom ones.


Pages:
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319