2012-12-16 61 views
1

我在使用GeoKit進行郵政編碼搜索時遇到問題。一些錯誤導致整個應用程序崩潰。Ruby on Rails:GeoKit郵編搜索?

這是我有:

def zipcode 
    zipcode = params[:zipcode] 
    @bathrooms = Bathroom.geo_scope(:all, :origin=>[zipcode], :within=>10) 
    respond_to do |format| 
     format.json { render :json => @bathrooms } 
     #format.json { render :json => {:bathrooms => @bathrooms} } 
     format.js { render :nothing => true } 
    end   
    end 




match '/bathrooms/zipcode', :controller => 'bathrooms', action =>"zipcode" 

這是我收到的錯誤:

ArgumentError in BathroomsController#zipcode 

wrong number of arguments (2 for 1) 

Rails.root: /Users/chance 1/source/rails_projects/squat 
Application Trace | Framework Trace | Full Trace 

app/controllers/bathrooms_controller.rb:44:in `geo_scope' 
app/controllers/bathrooms_controller.rb:44:in `zipcode' 

Request 

Parameters: 

{"zipcode"=>"47130", 
"format"=>"json"} 

Show session dump 

Show env dump 
Response 

Headers: 

任何幫助表示讚賞。

+0

您能否包含實際錯誤?你可以在Rails日誌(logs/development.log)中找到error/stacktrace –

+0

ActionController :: RoutingError(沒有路由匹配[GET]「/bathrooms/zipcode.json」): –

+0

Rendering /Library/Ruby/Gems/1.8 /modules/actionpackages/middleware/templates/rescues/routing_error.erb內救援/佈局(2.5ms) –

回答

0

geo_scope預計only one argument:散列。你傳遞了兩個參數:一個符號(:all)和一個散列(:origin=>[zipcode], :within=>10)。它接收兩個參數,它只是一個預期,給你的錯誤:

wrong number of arguments (2 for 1) 

有兩種方法來解決這個問題。

首先,你可以刪除:all符號,並使用查找方法:

Bathroom.geo_scope(:origin=>[zipcode], :within=>10).all 

或者,你可以完全忘記GeoKit和使用geocoder代替。 (github)

# GeoKit 
@bathrooms = Bathroom.geo_scope(:origin=>[zipcode], :within=>10).all 

# geocoder 
@bathrooms = Bathroom.near(zipcode, 10) 
+0

我嘗試了GeoKit的建議,現在我得到這個錯誤: 47130(數組)不能規範化爲一個LatLng 。我們嘗試將它解釋爲數組,字符串,可映射等,但沒有骰子。嗯???? –

+0

但這個工作... @bathrooms = Bathroom.geo_scope(:origin => params [:zipcode],:within => 10).all –

+0

感謝您的幫助! –