user1780370
user1780370

Reputation: 851

How can i get country name in Rails?

I am developing Rails 4 application where i tried lots for get user country means user open my site in which country that i want to get.

I tried

> 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

Any other way.

Thanks

Upvotes: 0

Views: 722

Answers (1)

Lavika
Lavika

Reputation: 530

We have used gem 'maxmind_geoip2', github: 'envato/geoip2' in our app. You need a mmdb file for it that you can get from the gem page.
Configure that gem as:
    MaxmindGeoIP2.file 'local_db_file.mmdb'
    MaxmindGeoIP2.locale 'en' # default: 'ru'

After that , you can get the IP of the user by using request.remote_ip in any request.

Just parse that IP using MaxmindGeoIP2.locate() . It will return you with a hash of the location details such as city, country, timezone etc..

Upvotes: 2

Related Questions