I've been trying to write some code that automatically detects the IP address of the website visitor and displays the corresponding city. I thought this would be as simple as using Geocoder to detect the IP then display the corresponding city, however I am having no success.
I've been looking through documentation that claims to be compatible with IP addresses, but either I'm doing something incorrectly, or the documentation is wrong: http://rdoc.info/github/alexreisner/geocoder/master/frames
I have this in my location.rb model, which supposedly provides data such as result.city
as a string.
class Location < ActiveRecord::Base
attr_accessible :address, :latitude, :longitude
reverse_geocoded_by :lat, :lon do |obj,results|
if geo = results.first
obj.city = geo.city
obj.zipcode = geo.postal_code
obj.country = geo.country_code
end
end
after_validation :reverse_geocode
end
So I added @city = result.city to my locations_controller.rb
def index
@locations = Location.all
@city = result.city
end
Then to test that it worked I put <%= @city %>
into my index.html.erb in locations.
Upon rendering the page I receive an error in my controller, stating that 'result' has not been defined:
undefined local variable or method `result' for #<LocationsController:0x007faf9d5009e8>
app/controllers/locations_controller.rb:7:in `index'
Where am I going wrong? Or why doesn't the model function as described?