Agile Web Development with Rails, Edition 4

Agile Web Development with Rails, Edition 4

24.3 Active Resources 21.2 Form Helpers

22 Caching

Restart the server.

curl --silent --head http://localhost:3000/
HTTP/1.1 200 OK 
Content-Type: text/html; charset=utf-8
Etag: "f64d3f69d8066b2fe50b1ef624ccad6d"
Cache-Control: max-age=0, private, must-revalidate
X-Ua-Compatible: IE=Edge
X-Runtime: 0.291512
Content-Length: 0
Server: WEBrick/1.3.1 (Ruby/1.9.4/2011-10-18)
Date: Tue, 18 Oct 2011 16:45:53 GMT
Connection: Keep-Alive
Set-Cookie: _depot_session=BAh7CEkiD3Nlc3Npb25faWQGOgZFRiIlOTNmMjRkNmRiY2E5NGQ4N2IwZWNjNzllNWI0MThlZjhJIgxjYXJ0X2lkBjsARmkPSSIQX2NzcmZfdG9rZW4GOwBGSSIxdUFVaDgrUnhKMFNQd0E4YXoyMkhnM0VOUmVNT2lYUjNPbFhSZlhSYTZFYz0GOwBG--14bd09894c8992c3624c83da59526974c63738c0; path=/; HttpOnly
 

add a method to return the latest product

edit app/models/product.rb

set ETAG and LastModified headers on the response

edit app/controllers/store_controller.rb
class StoreController < ApplicationController
  skip_before_filter :authorize
  def index
    if params[:set_locale]
      redirect_to store_path(locale: params[:set_locale])
    else
      @products = Product.order(:title)
      @cart = current_cart
    end
 
    latest = Product.latest
    fresh_when etag: latest, last_modified: latest.created_at.utc
    expires_in 10.minutes, public: true
  end
end
curl --silent --head http://localhost:3000/
HTTP/1.1 200 OK 
Etag: "726370df4784e59f28e5790f80be308e"
Last-Modified: Tue, 18 Oct 2011 16:39:15 GMT
Content-Type: text/html; charset=utf-8
Cache-Control: max-age=600, public
X-Ua-Compatible: IE=Edge
X-Runtime: 0.333435
Content-Length: 0
Server: WEBrick/1.3.1 (Ruby/1.9.4/2011-10-18)
Date: Tue, 18 Oct 2011 16:45:53 GMT
Connection: Keep-Alive
Set-Cookie: _depot_session=BAh7CEkiD3Nlc3Npb25faWQGOgZFRiIlNTFiMTAyNDhjNzVlNTQwNjI4OGM4YWE4MjFhZTE1ZTNJIgxjYXJ0X2lkBjsARmkQSSIQX2NzcmZfdG9rZW4GOwBGSSIxVFRHMTNOMWVJUk1hSmt6UlBiU2VnUXpudFpBbjJLT01Xb3hGb00xQTlLRT0GOwBG--cbe35c3b65fabb0c24a1eded3efed3c93df09180; path=/; HttpOnly
 
curl --silent --head http://localhost:3000/ -H 'If-None-Match: "726370df4784e59f28e5790f80be308e"'
HTTP/1.1 304 Not Modified 
Etag: "726370df4784e59f28e5790f80be308e"
Last-Modified: Tue, 18 Oct 2011 16:39:15 GMT
Cache-Control: max-age=600, public
X-Ua-Compatible: IE=Edge
X-Runtime: 0.213225
Server: WEBrick/1.3.1 (Ruby/1.9.4/2011-10-18)
Date: Tue, 18 Oct 2011 16:45:53 GMT
Connection: close
Set-Cookie: _depot_session=BAh7B0kiD3Nlc3Npb25faWQGOgZFRiIlNDE2ZjMxZjBiY2FjYTE4ZDg5YzY0Njk2ZDQ3NjQ4ZDZJIgxjYXJ0X2lkBjsARmkS--e5e7fd2054a6f7e205300db8edcc82bb92a691d3; path=/; HttpOnly
 
curl --silent --head http://localhost:3000/ -H 'If-Modified-Since: Tue, 18 Oct 2011 16:39:15 GMT'
HTTP/1.1 304 Not Modified 
Etag: "726370df4784e59f28e5790f80be308e"
Last-Modified: Tue, 18 Oct 2011 16:39:15 GMT
Cache-Control: max-age=600, public
X-Ua-Compatible: IE=Edge
X-Runtime: 0.244600
Server: WEBrick/1.3.1 (Ruby/1.9.4/2011-10-18)
Date: Tue, 18 Oct 2011 16:45:54 GMT
Connection: close
Set-Cookie: _depot_session=BAh7B0kiD3Nlc3Npb25faWQGOgZFRiIlMTEwYTZlYTY2Nzg1ZGJhYzllYWI1YmM5ZDVlOWM2ODBJIgxjYXJ0X2lkBjsARmkT--0f4170d9b6462ad6da98140763abbefa4b76e0eb; path=/; HttpOnly
 

Restart the server.

curl --silent --head http://localhost:3000/
HTTP/1.1 200 OK 
Etag: "726370df4784e59f28e5790f80be308e"
Last-Modified: Tue, 18 Oct 2011 16:39:15 GMT
Content-Type: text/html; charset=utf-8
Cache-Control: max-age=600, public
X-Ua-Compatible: IE=Edge
X-Runtime: 0.232832
Content-Length: 0
Server: WEBrick/1.3.1 (Ruby/1.9.4/2011-10-18)
Date: Tue, 18 Oct 2011 16:45:59 GMT
Connection: Keep-Alive
Set-Cookie: _depot_session=BAh7CEkiD3Nlc3Npb25faWQGOgZFRiIlMmM3YmU4NmVhN2QxZDg5MTYxNDk5YzZkY2QxZjlhMWVJIgxjYXJ0X2lkBjsARmkVSSIQX2NzcmZfdG9rZW4GOwBGSSIxZkQvTElWdnFGakpwTnJiclJRNFlZbFVsdDRTaHR2MTJzKzVEbEs1MWlIMD0GOwBG--9fdec5436d7ef9286ef4148966022db703de6f13; path=/; HttpOnly
 
curl --silent --head http://localhost:3000/ -H 'If-None-Match: "726370df4784e59f28e5790f80be308e"'
HTTP/1.1 304 Not Modified 
Etag: "726370df4784e59f28e5790f80be308e"
Last-Modified: Tue, 18 Oct 2011 16:39:15 GMT
Cache-Control: max-age=600, public
X-Ua-Compatible: IE=Edge
X-Runtime: 0.207316
Server: WEBrick/1.3.1 (Ruby/1.9.4/2011-10-18)
Date: Tue, 18 Oct 2011 16:46:00 GMT
Connection: close
Set-Cookie: _depot_session=BAh7B0kiD3Nlc3Npb25faWQGOgZFRiIlNzUwN2JkMjRlZDk4NDEyZjYwYzkyNjhjNTQ0MjBjYWZJIgxjYXJ0X2lkBjsARmkX--96eb947cb0efbfea2243ff9d5c455e484ebcf675; path=/; HttpOnly
 

24.3 Active Resources 21.2 Form Helpers