However, if @product is nil,
the helpers will not complain and will just render empty input fields. So, we can get
away with not initializing @product at all. Simply comment out the line initializing
@product, and the situation will be resolved:
# GET /products/new
# GET /products/new.xml
def new
#@product = Product.new
respond_to do |format|
format.html # new.erb
format.xml { render :xml => @product }
end
end
Now we start the client??™s web server on port 3000 (with the web service??™s web server
still running on port 4000):
$ cd products_example_client
$ script/server
Loading up http://localhost:3000/products in the browser, the application looks and
behaves the same, but it is backed by a RESTful connection over HTTPrather than a
SQL connection.
230 | Chapter 7: REST, Resources, and Web Services
We can examine the client??™s log to find a fairly standard-looking request and
response:
Processing ProductsController#index (for 127.0.0.1 at 2007-09-14
14:30:36) [GET]
Session ID: BAh7Bi...
Parameters: {"action"=>"index", "controller"=>"products"}
Rendering template within layouts/products
Rendering products/index
Completed in 1.02543 (0 reqs/sec) | Rendering: 0.19423 (18%) |
DB: 0.00000 (0%) | 200 OK [http://localhost/products]
But, as you may notice here, there is no ???Product Load??? entry indicating a database
call. If we look at the server??™s log, we notice that the client issued a GET request from
ActiveResource for http://localhost:4000/products.
Pages:
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359