Rails 地理编码器 gem 查找城市

Rails geocoder gem find city

我正在尝试查找用户的城市,现在我可以看到地址方法在我的控制器中正常工作

def your_location
  Geocoder.address(request.remote_ip)
end

但我不知道如何找到用户城市。

来自Geocoder GitHub

Geocoder adds location and safe_location methods to the standard Rack::Request object so you can easily look up the location of any HTTP request by IP address. For example, in a Rails controller or a Sinatra app:

# returns Geocoder::Result object
result = request.location

location 哈希示例:

{
  "data"=>{
    "ip"=>"213.174.0.254",
    "city"=>"Lviv",
    "latitude"=>49.8383,
    "zip_code"=>"79000",
    "longitude"=>24.0232,
    "time_zone"=>"Europe/Kiev",
    "metro_code"=>0,
    "region_code"=>"46",
    "region_name"=>"L'vivs'ka Oblast'",
    "country_code"=>"UA",
    "country_name"=>"Ukraine"
  },
  "cache_hit"=>nil
}