Search code examples
ruby-on-railsgmaps4railsrails-geocoder

Populate visitors IP address in the controller for Geocoder near() query


I want to grab the visitors IP address and use it in my controller so that I can use geocoder to find nearby objects.

So I have a Location model, and I want to run a query like this:

def index
  @json = Location.near('visitor location here', 20).to_gmaps4rails
end

So I can show them a map pre-populated with the closest locations.

How do I access their IP address and use it in the controller? Can it be done? Should I be trying to accomplish this in the model using a scope?


Solution

  • From your controller:

    request.remote_ip