簡體   English   中英

Rails + Geocoder:Geokit :: Geocoders :: Geocoder:Class的未定義方法“搜索”

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

我正在玩Geocoder gem ,我將此代碼添加到了用戶模型中

  include GeoKit::Geocoders

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

在另一個控制器中,我在CarController#index中調用:

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

但是代替

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

我有

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

Geocoder的設置有何不正確之處?

我認為這只是一個名稱空間問題。 您需要刪除include GeoKit::Geocoders 我看着在地理編碼器源,並不能找到任何GeoKit

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

所以我用谷歌搜索,顯然還有另一種叫做geokit的寶石。 您提到的名稱空間在第92行的這個gem中得到了解決。 而且,此GeoCoder類顯然沒有搜索方法。 因此,我懷疑您在Gemfile中引用了geokit。

順便說一句,我只使用地理編碼器,而且在我的環境中也可以正常工作-

$ 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 >

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM