开发者

Geokit helper usage

开发者 https://www.devze.com 2023-02-28 10:04 出处:网络
I\'m trying to localize my users throught their IP address. As the docs say, a class method called geocode_ip_address has been mixed into the ActionController::Base. But there must be something I\'m m

I'm trying to localize my users throught their IP address. As the docs say, a class method called geocode_ip_address has been mixed into the 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 every request don开发者_开发知识库e).

The documentation also talks about "A first-time lookup will result in the GeoLoc class being stored in the session as :geo_location" but I certainly don't have that key inside the session hash.

What am I doing wrong?

Thanks.


You don't need to prepend before_filter to geocode_ip_address, but rather just put that in 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

Note that if the geocoding fails geo will be nil. If you're running in development you'll be trying to geocode 127.0.0.1, so you'll want to set your remote_ip in the request object. I did so by adding this to the bottom of config/environments/development.rb:

class ActionDispatch::Request
  def remote_ip
    "x.x.x.x" # fill in your IP address here
  end
end
0

精彩评论

暂无评论...
验证码 换一张
取 消