2015-06-21 61 views
1

我有他們輸入的用戶的郵政編碼,並且想要預先填充城市/州的幾個字段。有誰知道獲取這些信息的好方法嗎?我想要做這樣的事情:Rails從ZIP獲取狀態Geoip

GeoIP.new('geoip/GeoLiteCity.dat').zip('53593') 

我知道我能做到GeoIP.new('geoip/GeoLiteCity.dat').city(request.remote_ip)但是這也正是他們在,而不是他們實際上可能居住的時間。

+0

鏈接到這個庫? – Chloe

回答

3

我不認爲geoip紅寶石(Github repo)有能力通過郵政編碼進行搜索。當掃描the geoip.rb source時,沒有方法zippostal_code

還有另一個寶石,my_zipcode_gem,你可以使用。這也是hosted on Github和語法如下:

zipcode = Zipcode.find_by_code '66206' 
zipcode.state.abbr # => 'KS' 
zipcode.city   # => 'Shawnee Mission' 
zipcode.county.name # => 'Johnson' 
zipcode.lat.to_s  # => '38.959356', it is actually a BigDecimal object converted to_s for documentation. 
zipcode.lon.to_s  # => '-94.716155', ditto 
zipcode.is_geocoded? # => true, most if not all should be pre-geocoded.