Geocoder, how to test locally when ip is 127.0.0.1?

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

    I implemented this slightly different, and this works well for my case.

    In application_controller.rb i have a lookup method which calls the Geocoder IP lookup directly passing in the results of request.remote_ip.

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

    Then in config/environments/development.rb i monkey-patched the remote_ip call:

    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
    

    I just hard code some addresses, but you could do whatever you'd like here.

提交回复
热议问题