Geocoder, how to test locally when ip is 127.0.0.1?

前端 未结 6 1796
盖世英雄少女心
盖世英雄少女心 2020-12-04 18:16

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\"

6条回答
  •  难免孤独
    2020-12-04 19:08

    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:

    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
    

提交回复
热议问题