Geocoder, how to test locally when ip is 127.0.0.1?

前端 未结 6 1791
盖世英雄少女心
盖世英雄少女心 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:20

    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
    

    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')
    

提交回复
热议问题