13

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?

4

6 回答 6

21

一个很好的干净方法是使用 MiddleWare。将此类添加到您的 lib 目录:

# 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 文件中:

config.middleware.use('SpoofIp', '64.71.24.19')
于 2012-01-18T20:51:28.783 回答
5

为此,我通常使用params[:ip]或正在开发中的东西。这使我可以测试其他 IP 地址的功能并假装我在世界任何地方。

例如

class ApplicationController < ActionController::Base
  def request_ip
    if Rails.env.development? && params[:ip]
      params[:ip]
    else
      request.remote_ip
    end 
  end
end
于 2011-05-24T19:37:45.287 回答
4

我实现了这个略有不同,这对我的情况很有效。

application_controller.rb我有一个查找方法,它调用 Geocoder IP 查找直接传递 request.remote_ip 的结果。

def lookup_ip_location
  if Rails.env.development?
    Geocoder.search(request.remote_ip).first
  else
    request.location
  end
end

然后在config/environments/development.rb我猴子修补了 remote_ip 调用:

class ActionDispatch::Request
  def remote_ip
    "71.212.123.5" # ipd home (Denver,CO or Renton,WA)                                                                                                                                                                                                                                                                        
    # "208.87.35.103" # websiteuk.com -- Nassau, Bahamas                                                                                                                                                                                                                                                                      
    # "50.78.167.161" # HOL Seattle, WA                                                                                                                                                                                                                                                                                       
  end
end

我只是硬编码了一些地址,但你可以在这里做任何你想做的事情。

于 2013-06-11T00:46:01.833 回答
1

我有同样的问题。这是我使用地理编码器实现的方式。

#gemfile
gem 'httparty', :require => 'httparty', :group => :development 

#application_controller
def request_ip
  if Rails.env.development? 
     response = HTTParty.get('http://api.hostip.info/get_html.php')
     ip = response.split("\n")
     ip.last.gsub /IP:\s+/, ''      
   else
     request.remote_ip
   end 
end

#controller
ip = request_ip
response = Geocoder.search(ip)

(来自 geo_magic gem 的 hostip.info 的代码部分,并基于这个问题的另一个答案。)

现在你可以做类似的事情response.first.state

于 2011-06-21T22:16:53.190 回答
1

这是地理编码器 1.2.9 的更新答案,为开发和测试环境提供硬编码 IP。只需将其放在您的底部config/initilizers/geocoder.rb

if %w(development test).include? Rails.env
  module Geocoder
    module Request
      def geocoder_spoofable_ip_with_localhost_override
        ip_candidate = geocoder_spoofable_ip_without_localhost_override
        if ip_candidate == '127.0.0.1'
          '1.2.3.4'
        else
          ip_candidate
        end
      end
      alias_method_chain :geocoder_spoofable_ip, :localhost_override
    end
  end
end
于 2015-08-24T20:16:31.813 回答
-1

你也可以这样做

request.safe_location
于 2016-03-10T05:12:59.907 回答