Prev | Current Page 340 | Next

Brad Ediger

"Advanced Rails"


The advantage of using standardized codes, of course, is that everyone (theoretically)
uses them and understands them. In particular, Rails uses a rich set of response
codes when requesting and providing data over a RESTful interface. ActiveResource,
for example, will respond to a 404 by raising ActiveResource::ResourceNotFound (a
parallel of ActiveRecord::RecordNotFound), and to a 422 by raising ActiveResource::
ResourceInvalid (upon failed validations).
ActiveResource: Consuming RESTful Services
One huge advantage of coding web services RESTfully is that clients can then consume
them using a standard interface. ActiveResource is a library, now part of edge
Rails, which abstracts RESTful resources using an ActiveRecord-like interface. It fits
well into the Rails model of building applications, and as long as the server was built
using RESTful Rails conventions, using ActiveResource is nearly transparent.
To demonstrate the power of ActiveResource, we will build a very simple application
(nothing more than a scaffolded interface to a set of ActiveRecord objects) to
manage a set of products in a fictional store. We will first build this as a basic
ActiveRecord application using RESTful principles; then we will use ActiveResource
to disconnect the backend web service from the front end interface.
224 | Chapter 7: REST, Resources, and Web Services
We will be using the scaffold generator (formerly scaffold_resource) to
build some quick RESTful templates for the interface.


Pages:
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352