Get real IP address in local Rails development environment

前端 未结 8 566
眼角桃花
眼角桃花 2020-11-30 23:15

I have Rails 2.3.8, Ruby 1.8.7, Mongrel Web Server and MySQL database.

I am in the development mode and I need to find the real IP address

相关标签:
8条回答
  • 2020-11-30 23:42

    If you access the development machine with your real IP you should get your real IP, so don't use localhost, but your use your real IP. Not all routers are will allow LAN access to a machine to an external IP address that is actually on that LAN, but most will.

    0 讨论(0)
  • 2020-11-30 23:42

    Problem:

    We were trying to do a similar thing for a project recently and were having trouble using request.remote_ip in our code. It kept returning 127.0.0.1 or ::1. When combined with the Geocoder gem it was giving us an empty array and was basically useless.

    Solution:

    There's a really nice API at telize.com which returns the IP address of whatever hits it. The code we implemented looked something like this:

    location = Geocoder.search(HTTParty.get('http://www.telize.com/ip').body)

    It's not the most rails-y way to do it, but I think this is a pretty good solution since it will work locally and on production as well. The only potential problem is if you hit their API limit, but for small projects this should be a non-issue.

    0 讨论(0)
提交回复
热议问题