Geokit 帮助程序的使用

发布于 2024-11-02 13:39:13 字数 332 浏览 1 评论 0原文

我正在尝试通过 IP 地址来本地化我的用户。正如文档所述,名为 geocode_ip_address 的类方法已混合到 ActionController::Base 中。但一定有什么东西我错过了。我是否必须定义一个像 before_filter :geocode_ip_address 这样的过滤器才能使用它? (我想知道每个请求完成的位置)。

该文档还谈到“首次查找将导致 GeoLoc 类作为 :geo_location 存储在会话中”,但我当然在会话哈希中没有该密钥。

我做错了什么?

谢谢。

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 done).

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

勿忘初心 2024-11-09 13:39:13

您不需要在 geocode_ip_address 前面添加 before_filter ,而只需将其放入控制器中即可:

class YourController < ApplicationController
  geocode_ip_address

  def action
    if geo = session[:geo_location]
      # geo here is a Geokit::GeoLoc object
    end
  end
end

请注意,如果地理编码失败,geo 将为零。如果您在开发中运行,您将尝试对 127.0.0.1 进行地理编码,因此您需要在请求对象中设置您的remote_ip。我通过将其添加到 config/environments/development.rb 的底部来做到这一点:

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文