【问题标题】:How can i get country name in Rails?如何在 Rails 中获取国家名称?
【发布时间】:2016-04-14 10:52:07
【问题描述】:

我正在开发Rails 4 application,在那里我尝试了很多获取用户国家意味着用户打开我的网站,我想在其中获得country

我试过了

> 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

任何其他方式。

谢谢

【问题讨论】:

    标签: ruby-on-rails country


    【解决方案1】:

    我们在我们的应用程序中使用了gem 'maxmind_geoip2', github: 'envato/geoip2'。 您需要一个可以从 gem 页面获取的 mmdb 文件。
    将该 gem 配置为:
    MaxmindGeoIP2.file 'local_db_file.mmdb'
    MaxmindGeoIP2.locale 'en' # 默认值:'ru'

    之后,您可以在任何请求中使用 request.remote_ip 获取用户的 IP。

    只需使用 MaxmindGeoIP2.locate() 解析该 IP。它会返回一个包含位置详细信息的哈希值,例如城市、国家/地区、时区等。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-06-11
      • 2020-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多