0

I want to get the real IP of my system so that I can further get the location(using geocoder gem). I am using devise gem's trackable module to get the IP of the user. When I try to geocode the IP, it fails because the last_signed_in_ip returned by devise is always 127.0.0.1. I double checked the location permissions in the browser so that doesnt seem to be an issue as it is allowed to track location there. What can be wrong?

Also, is there a better way to find out one's location (apart from tracking IP address and then geocoding it?). Can the user be asked again to allow location tracking from within the application in case he has not allowed it or isnt aware of such setting?

Appreciate all the help in advance.

Thanks

3
  • Do you run your application behind proxy server like nginx or haproxy? Oct 26, 2013 at 16:43
  • No, this is just my development environment and there is no proxy... Oct 27, 2013 at 4:48
  • Do you access your app via http://localhost or http://127.0.0.1 ? Oct 27, 2013 at 20:20

2 Answers 2

0

request.remote_ip will solve your problem...

it will never return you your own m/c address, instead If you need the remote address for (testing) your geocoding, I suggest adding 127.0.0.1 to your database table that matches IP addresses with locations

you can hardcode like this for testing purpose:

if request.remote_ip == '127.0.0.1'
  # Hard coded remote address
  '123.45.67.89'
else
  request.remote_ip
end
1
  • unfortunately it doesnt. Thats exactly what devise uses but for some reason it doesnt return the true address Oct 26, 2013 at 8:51
0

If you want to return your external IP address programmatically then you need to access your app from that address.

When you visit your app at localhost, request.remote_ip will return 127.0.0.1 because that is your localhost IP address.

To get around this you can either forward your development port (normally 3000) from your router to your PC and then access your app by entering your external IP address into your browser instead of localhost (eg: 123.123.123.123:3000).

Alternatively add your external IP address to your hosts file pointing to localhost (ie: localhost 123.123.123.123) and then browse to your app via your external IP address as above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.