Rails all the way

Entries categorized as ‘rails’

time_zone_select showing only US time zones

January 4, 2008 · 1 Comment

time_zone_select produces option tags for almost any time zones in the world but if you want the list of US time zones only, without using the TZInfo library then you should add a file under the lib directory say us.rb and add the following code

class US
 def self.all
  TimeZone.us_zones
 end
end

and in you views use

<%= time_zone_select object, method, nil, :model=>US %>

Categories: rails
Tagged:

Etag in Edge Rails

September 3, 2007 · No Comments

What is an ETag and How does it work??

When a request is made for the first time then during page render, etag header is automatically inserted on 200 OK responses. The etag is calculated using MD5 of the response body.
On subsequent requests the etag is send along as request.headers['HTTP_IF_NONE_MATCH'] and compared with response.headers['ETAG'] . If they are equal then the response is changed to a 304 Not Modified and the response body is set to an empty string.

So how does that help??

ETag is used to expire the browser caching and for saving transfer of course!!
- aggregators can tell which version of feed they have and thus server sends data only if some newer version is there.
- no more cache based on page expiration.

A ruby snippet for showing ETags

require ‘net/http’
# my rails app running on edge rails
http = Net::HTTP.new(”localhost”, 3000)


# /controller/action
data = http.get2(”/say”)


p data.body
p data.header.each{|k,v| p “#{k} = #{v}”}


data2 = http.request_get(”/say”, {’If-None-Match’=>data.header['etag']})


p data2.body
p data2.header.each{|k,v| p “#{k} = #{v}”}
# 304 not modified n body empty coz of no change in content

Categories: Ruby · rails
Tagged: