ruby on rails - Autofilling a form with visitor's country using IP address -
this first time i'm doing this. have form new users (investor model) , want once form loaded, based on visitor's ip, country
field filled country. heard of geoip
gem. don't know how use it. tried do. downloaded geoip.dat.gz
http://www.maxmind.com/app/geolitecountry
, extracted , put db
folder of app. not sure if on right path.
gemfile
source 'https://rubygems.org' gem 'rails', '4.2.1' gem 'pg' gem 'sass-rails', '~> 5.0' gem 'uglifier', '>= 1.3.0' gem 'coffee-rails', '~> 4.1.0' gem 'jquery-rails' gem 'turbolinks' gem 'jbuilder', '~> 2.0' gem 'sdoc', '~> 0.4.0', group: :doc gem 'foundation-rails', '~> 5.5.2.1' gem 'foundation-icons-sass-rails' gem 'geoip', '~> 1.5.0' # use activemodel has_secure_password # gem 'bcrypt', '~> 3.1.7' # use unicorn app server # gem 'unicorn' # use capistrano deployment # gem 'capistrano-rails', group: :development gem 'rails_12factor', group: :production group :development, :test # call 'byebug' anywhere in code stop execution , debugger console gem 'byebug' # access irb console on exception pages or using <%= console %> in views gem 'web-console', '~> 2.0' # spring speeds development keeping application running in background. read more: https://github.com/rails/spring gem 'spring' end
environment.rb
# load rails application. require file.expand_path('../application', __file__) # initialize rails application. rails.application.initialize! config.gem 'geoip'
the following steps shows cannot load such file -- geoip
error in browser. , can't move on trying interpolate user's country in form. appreciated. investors_controller.rb
require 'geoip' class investorscontroller < applicationcontroller @geoip ||= geoip.new("/db/geoip.dat") remote_ip = request.remote_ip if remote_ip != "127.0.0.1" #todo: check other local addresses or set default value location_location = @geoip.country(remote_ip) if location_location != nil @investor.country = location_location[2] end end def new @investor = investor.new end def create @investor = investor.new(user_params) if @investor.save flash.now[:success] = "welcome, you're on way success!" render "/static_pages/welcome" investormailer.welcome_email(@investor).deliver_now else render 'new' end end def update if @investor.update_attributes(user_params) flash[:success] = "updated" else render 'edit' end end private def user_params params.require(:investor).permit(:name, :email, :country, :phone) end end
i've had lot of success geocoder gem. works right out of box:
in gemfile, add:
gem geocoder
once bundle install, can queries against freegeoip (up 10,000 per hour) right in app. here's example console, using google's dns ip:
013 > results = geocoder.search("8.8.4.4") => [#<geocoder::result::freegeoip:0x007fa285fddfc0 @data={"ip"=>"8.8.4.4", "country_code"=>"us", "country_name"=>"united states", "region_code"=>"", "region_name"=>"", "city"=>"", "zip_code"=>"", "time_zone"=>"", "latitude"=>38, "longitude"=>-97, "metro_code"=>0}, @cache_hit=false>] 014 > results.first.data["country_name"] => "united states"
there lot of other useful data geocoder gem provides well, can see. :)
i strongly recommend read entire geocoder readme give tips, suggestions on services can configure address lookup if wish, practices testing, etc.
i'd suggest once narrow down services want at, reading terms of services services (e.g. google maps, bing, etc.) readme geocoder gem isn't date policies.
Comments
Post a Comment