0

I am trying to display a list of salons that correspond to the users location on the show view of treatment.

The two tables don't have a direct relationship.

I have installed the geokit gem and now I am trying to work out if I need to add additional columns also to salon and user to store their respective co-ordinates(lat,lng) so that I can compare them using a scope.

How to I retrieve the users location via their IP and store it?

If someone could show me some example code of what the model/view and controllers should look like it would be of great help.

1 Answers1

0

You can get the location using geokit-rails gem by following way

location = IpGeocoder.geocode('12.215.42.19')

The ip is 12.215.42.19

Also

The Multi-Geocoder will also geocode IP addresses and provide failover among multiple IP geocoders. Just pass in an IP address for the parameter instead of a street address. Eg:

location = Geocoders::MultiGeocoder.geocode('12.215.42.19')
Rajarshi Das
  • 11,778
  • 6
  • 46
  • 74
  • I have the following in my application controller just to test with `def ip @user_location = IpGeocoder.geocode(request.remote_ip) end` and `It appears you're located in <%= @user_location.city%>.` in my users/index.html.erb file but i am getting an error of `undefined method `city' for nil:NilClass` – Fergus Morton Dec 08 '15 at 16:07
  • you can use `try` like `<%= @user_location.try(:city)%>` – Rajarshi Das Dec 09 '15 at 06:52