Question

How to get ip address from your country location in rails?

I try typing request.remote_ip in console but I get ip address 127.0.0.1, this is localhost?... I won't to get ip address from country user derived.

I'm using gem named geokit-rails for getting country based ip address.

To get ip address from your location if i try this code :

Model.geocode(ip-addess)

thanks in advance.

Was it helpful?

Solution 2

To use a development host's external IP address rather than the "localhost" address or loopback address of 127.0.0.1, explicitly bind to the host's external IP address. By default servers may listen on 0.0.0.0 (any address) and the localhost address ends up being used when testing on the same host as the development host.

To bind explicitly using web brick for example:

rails server -b my.real.ip.address

To bind explicitly using apache/passenger or similar, in the apache config use:

Listen my.real.ip.address:port

OTHER TIPS

You can also use:

  class ActionDispatch::Request
    def remote_ip
      "38.89.128.21"
    end
  end

in config/environments/development to spoof your IP to NYC (or anywhere you set the IP to be) when it's just going to be your local machine accessing it. You're overwriting that function. This is useful if you're doing geolocation stuff with the IP addresses.

Edit: I accidentally wrote /production instead of /development. Don't do that. Bad idea, all of your users would be from NY.

It is request.remote_ip that gives you the "Originating IP address". Because in your development machine your server and client are the same you are getting 127.0.0.1.

To confirm this visit your rails application from a different machine.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top