Ruby on rails 开发环境中地理编码器的假IP

Ruby on rails 开发环境中地理编码器的假IP,ruby-on-rails,ip-address,development-environment,rails-geocoder,Ruby On Rails,Ip Address,Development Environment,Rails Geocoder,我想替换开发环境中的127.0.0.1IP,以便手动测试geocoder gem。我该怎么做 我试过了,但没用。我遇到以下错误: .rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.0.0.rc1/lib/active\u support/inco‌​ctor/methods.rb:226:in'const_get':未初始化的常量欺骗(NameError) 将其添加到您的config/environments/development.rb中 class

我想替换开发环境中的127.0.0.1IP,以便手动测试geocoder gem。我该怎么做

我试过了,但没用。我遇到以下错误:

.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.0.0.rc1/lib/active\u support/inco‌​ctor/methods.rb:226:in'const_get':未初始化的常量欺骗(NameError)

将其添加到您的config/environments/development.rb中

class ActionController::Request
  def remote_ip
    "1.2.3.4"
  end
end

重新启动服务器。

这是正确答案:

class ActionDispatch::Request
  def remote_ip
    "1.2.3.4"
  end
end

我的答案并不适用于所有人,因为我只想在本地测试
request.location
。但是如果你想做同样的事情,那么我有解决办法。首先,让我向您展示
.location
方法的源代码:

module Geocoder
  module Request

    def location
      @location ||= begin
        detected_ip = env['HTTP_X_REAL_IP'] || (
          env['HTTP_X_FORWARDED_FOR'] &&
          env['HTTP_X_FORWARDED_FOR'].split(",").first.strip
        )
        detected_ip = IpAddress.new(detected_ip.to_s)
        if detected_ip.valid? and !detected_ip.loopback?
          real_ip = detected_ip.to_s
        else
          real_ip = self.ip
        end
        Geocoder.search(real_ip).first
      end
      @location
    end
  end
end

# ...
如您所见,存在变量
检测到的\u ip
,要找到它的数据,gem将检查
env['HTTP\u X\u REAL\u ip']
。好的,现在我们可以很容易地将其存根到控制器中:

class HomeController < ApplicationController    
  def index
    env['HTTP_X_REAL_IP'] = '1.2.3.4' if Rails.env.development?
    location = request.location

    # => #<Geocoder::Result::Freegeoip:0x007fe695394da0 @data={"ip"=>"1.2.3.4", "country_code"=>"AU", "country_name"=>"Australia", "region_code"=>"", "region_name"=>"", "city"=>"", "zipcode"=>"", "latitude"=>-27, "longitude"=>133, "metro_code"=>"", "area_code"=>""}, @cache_hit=nil> 
  end
end
class HomeController“1.2.3.4”、“国家代码”=>“AU”、“国家名称”=>“澳大利亚”、“地区代码”=>,“地区名称”=>,“城市”=>,“zipcode”=>,“纬度”=>-27,“经度”=>133,“地铁代码”=>,“地区代码”=>,@cache\u hit=nil>
结束
结束

它与geocoder“1.2.5”配合使用(不能保证它与早期版本配合使用-您需要查看该版本的源代码或升级gem)。

这是geocoder 1.2.9为开发和测试环境提供硬编码IP的更新答案:

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

下面这篇文章有一个类似的问题和一个很好的答案。是的,但对我不起作用。也许还有其他方法可以做到这一点?在Dev环境中重写requst.remote_ip方法如何?好的,但你们如何才能做到这一点,只有在开发环境中?我已经添加了它作为一个答案。谢谢!但是为什么
ActionController
而不是
ActionDispatch
?它不起作用:-(如果我像
Current Location:
那样做,页面只呈现
Current Location:
那么好,非常感谢!我会把
env[HTTP\u X\u REAL\u IP]
ApplicationController中的before\u过滤器中,当然只在开发中使用它。Geocoder 1.2.6.使用
env['REMOTE\u ADDR']