Geokit 帮助程序的使用
我正在尝试通过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不需要在 geocode_ip_address 前面添加 before_filter ,而只需将其放入控制器中即可:
请注意,如果地理编码失败,geo 将为零。如果您在开发中运行,您将尝试对 127.0.0.1 进行地理编码,因此您需要在请求对象中设置您的remote_ip。我通过将其添加到 config/environments/development.rb 的底部来做到这一点:
You don't need to prepend before_filter to geocode_ip_address, but rather just put that in your controller:
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: