Search code examples
ruby-on-railsrubygeolocationrails-geocoder

Rails + Geocoder: undefined method `search' for Geokit::Geocoders::Geocoder:Class


I am playing with the Geocoder gem, I added this code to user model

  include GeoKit::Geocoders

  geocoded_by :full_street_address   # can also be an IP address
  after_validation :geocode          # auto-fetch coordinates

and in a different controller, in the CarController#index I am calling:

zip_code = '94301'
aaa = Geocoder.search(zip_code).first.coordinates
puts '==='
puts aaa.inspect
puts '==='

But instead of

s[0].latitude   # =>    37.4457966
s[0].longitude  # =>    -122.1575745
s[0].address    # =>    "Palo Alto, CA 94301, USA"

I got

undefined method `search' for Geokit::Geocoders::Geocoder:Class

What's incorrect with the setup of Geocoder?


Solution

  • I think its just a namespace issue. You need to remove include GeoKit::Geocoders. I looked in geocoder source and can't find anything GeoKit

    $ pwd
    /usr/local/rvm/gems/ruby-2.0.0-p247/gems/geocoder-1.1.8/lib
    $ find . | xargs grep -i GeoKit
    $
    

    So I googled and apparently there's another gem called geokit. The namespace you've mentioned gets resolved in this gem at line 92. And this GeoCoder class obviously doesn't have a search method. So I suspect you've a reference to geokit in your Gemfile.

    And btw I use just geocoder & it works fine on my environment -

    $ rails console
    Loading development environment (Rails 4.0.0)
    2.0.0-p247 :001 > zip_code = '94301'
     => "94301"
    2.0.0-p247 :002 > aaa = Geocoder.search(zip_code).first.coordinates
     => [37.4457966, -122.1575745]
    2.0.0-p247 :003 >