Add GeoIP gem to your Gemfile and run bundle install:
[cc lang=”ruby”]
gem ‘geoip’
[/cc]
Download GeoIP.dat.gz from http://www.maxmind.com/app/geolitecountry and unzip it in “db” folder. Initialize geoip with downloaded file path and get location-based on request IP:
[cc lang=”ruby”]
@geoip ||= GeoIP.new(“#{RAILS_ROOT}/db/GeoIP.dat”)
remote_ip = request.remote_ip
location = @geoip.country(remote_ip)[:country_name]
[/cc]