In addition, in Rails 2.0, ActiveResource treats a
422 response as a failed validation.
Now we will turn this simple web application into a client/server application. Here is
where the magic happens. We will disconnect the backend (ActiveRecord and database
access) from the front end (the user interface). The backend will become the
server, and the front end will become the client. The two will talk over HTTPusing a
RESTful interface.
As the application is already set up to render XML when requested (via an Accept
header of text/xml or a format extension of .xml), no changes are needed to the server.
We can treat the server as a web service simply by requesting XML over HTTP. The
client will require some slight modifications to talk over a RESTful HTTPinterface
rather than a database connection, but the changes will be very small as ActiveResource
was designed to have an ActiveRecord-like interface.
First, we will copy the entire project (the original of which will become the server)
into a separate directory, which will become the client:
$ cp -R products_example products_example_client
As mentioned previously, the server needs no modification at all. We can start it on
port 4000, which will become the location at which we access the web service:
$ cd products_example_client
$ script/server --port 4000
Now we can open up the code for the client and modify it to query http://localhost:
4000/ for products.
Pages:
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357