本文介绍了Geocoder,ip为127.0.0.1时如何在本地测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我无法让地理编码器正常工作,因为我的本地 IP 地址是 127.0.0.1,所以它无法定位到我正确的位置.
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.
request.location.ip 显示127.0.0.1"
The request.location.ip shows "127.0.0.1"
如何使用不同的 ip 地址(我的互联网连接 ip)来破坏更多相关数据?
How can I use a different ip address (my internet connection ip) so it will bring break more relevant data?
推荐答案
使用 MiddleWare 是一种干净利落的方式.将此类添加到您的 lib 目录中:
A nice clean way to do it is using MiddleWare. Add this class to your lib directory:
# lib/spoof_ip.rb
class SpoofIp
def initialize(app, ip)
@app = app
@ip = ip
end
def call(env)
env['HTTP_X_FORWARDED_FOR'] = nil
env['REMOTE_ADDR'] = env['action_dispatch.remote_ip'] = @ip
@status, @headers, @response = @app.call(env)
[@status, @headers, @response]
end
end
然后找到您要用于开发环境的 IP 地址并将其添加到您的 development.rb 文件中:
Then find an IP address you want to use for your development environment and add this to your development.rb file:
config.middleware.use('SpoofIp', '64.71.24.19')
这篇关于Geocoder,ip为127.0.0.1时如何在本地测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!