2016-04-14 30 views
0

我正在開發Rails 4 application我在那裏試圖獲取用戶國家意味着用戶打開我的網站,其中country,我想要得到的。如何在Rails中獲取國家名稱?

我試圖

> Geocoder where too much load and not get proper output 
> jQuery /Javascript code but always ask to user for share location which one is not good way 
> geoip where get only ip country which always return server ip location not user browser location 

任何其他方式。

感謝

回答

2

我們已經在我們的應用程序使用gem 'maxmind_geoip2', github: 'envato/geoip2'。 您需要一個可從gem頁面獲取的mmdb文件。
配置創業板爲:
    MaxmindGeoIP2.file 'local_db_file.mmdb'
    MaxmindGeoIP2.locale '恩' #默認: '儒'

之後,你可以得到的知識產權用戶通過在任何請求中使用request.remote_ip。

只需使用MaxmindGeoIP2.locate()解析IP即可。它會返回給你一個哈希的位置詳細信息,如城市,國家,時區等。