Geocoder,当ip为127.0.0.1时如何在本地测试?
我无法让地理编码器正常工作,因为我的本地 IP 地址是 127.0.0.1,因此它无法正确定位我所在的位置。
request.location.ip 显示“127.0.0.1”
我如何使用不同的 IP 地址(我的互联网连接 IP),以便它会带来更多相关数据?
I can't get geocoder to work correct as my local ip address is 127.0.0.1 so it can't located where I am correctly.
The request.location.ip shows "127.0.0.1"
How can I use a different ip address (my internet connection ip) so it will bring break more relevant data?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
一个很好的干净方法是使用 MiddleWare。将此类添加到您的 lib 目录:
然后找到您想要用于开发环境的 IP 地址并将其添加到您的development.rb 文件中:
A nice clean way to do it is using MiddleWare. Add this class to your lib directory:
Then find an IP address you want to use for your development environment and add this to your development.rb file:
为此,我通常使用
params[:ip]
或正在开发的东西。这使我能够测试其他 IP 地址的功能并假装我在世界任何地方。例如
For this I usually use
params[:ip]
or something in development. That allows me to test other ip addresses for functionality and pretend I'm anywhere in the world.For example
我的实现略有不同,这对我的情况很有效。
在
application_controller.rb
中,我有一个查找方法,它调用Geocoder IP查找,直接传入request.remote_ip的结果。然后在 config/environments/development.rb 中,我对remote_ip 调用进行了猴子修补:
我只是硬编码了一些地址,但您可以在这里做任何您想做的事情。
I implemented this slightly different, and this works well for my case.
In
application_controller.rb
i have a lookup method which calls the Geocoder IP lookup directly passing in the results of request.remote_ip.Then in
config/environments/development.rb
i monkey-patched the remote_ip call:I just hard code some addresses, but you could do whatever you'd like here.
我也有同样的问题。这是我使用地理编码器实现的方法。
(使用 geo_magic gem 中的 hostip.info 进行代码部分,并基于此问题的其他答案。)
现在您可以执行类似
response.first.state
的操作I had the same question. Here is how I implemented with geocoder.
( code part with hostip.info from geo_magic gem, and based on the other answer to this question. )
now you can do something like
response.first.state
这是地理编码器 1.2.9 的更新答案,为开发和测试环境提供硬编码 IP。只需将其放在
config/initilizers/geocoder.rb
的底部即可:This is an updated answer for geocoder 1.2.9 to provide a hardcoded IP for development and test environments. Just place this at the bottom of your
config/initilizers/geocoder.rb
:你也可以这样做
you may also do this