Using the Geokit Assistant

I am trying to localize my users by their IP address. As the docs say, a class method called geocode_ip_address was mixed in ActionController::Base . But there must be something I'm missing. Do I have to define a filter like this before_filter :geocode_ip_address to use it? (I want to know the location for each request made).

The documentation also says that "The first time the search results in the GeoLoc class being saved in the session as :geo_location ", but I certainly do not have this key inside the hash session.

What am I doing wrong?

Thanks.

+4
source share
1 answer

You do not need to add before_filter to geocode_ip_address, but just add this to your controller:

 class YourController < ApplicationController geocode_ip_address def action if geo = session[:geo_location] # geo here is a Geokit::GeoLoc object end end end 

Please note that if geo coding failure geo will be zero. If you are working in development, you will try to geocode 127.0.0.1, so you need to set your remote_ip to the request object. I did this by adding this to the end of config / environment / development.rb :

 class ActionDispatch::Request def remote_ip "xxxx" # fill in your IP address here end end 
+5
source

All Articles