Typical usage is as follows:
def update
@product = Product.find params[:id]
if_unmodified @product do
if @product.update_attributes params[:product]
redirect_to product_path(@product)
else
render
end
end
end
Now, before updating the product with the provided attributes, the headers will be
inspected to ensure that the client and server agree on the resource??™s state before the
update. If they are the same, the PUT will proceed as usual.
Note that the typical application of this code has a race condition
between if_unmodified checking the headers and actually performing
the update. This is unavoidable from the plugin??™s standpoint, as it has
no idea what you will be doing inside the block.
To ensure that this race condition doesn??™t cause problems under heavy
concurrency, you will need to wrap the entire SELECT/UPDATE series in a
database transaction, and run the database under a transaction isolation
level that prevents nonrepeatable reads (such as SERIALIZABLE).
HTTP Response Status Codes
One often-overlooked part of HTTPis the rich set of response status codes it defines.
The HTTP/1.1 RFC defines many response codes that are appropriate for documentbased
interactions; this set was enriched by WebDAV with some status codes that
filled in the gaps for dynamic web applications (such as 422 Unprocessable Entity,
used when the submitted entity is semantically invalid, often as determined by
ActiveRecord validations).
Pages:
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350