簡體   English   中英

Ruby on Rails:GeoKit郵政編碼搜索?

[英]Ruby on Rails: GeoKit Zipcode Search?

我在獲取郵政編碼搜索以與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: 

任何幫助表示贊賞。

geo_scope 只需要一個參數 :哈希。 您為其傳遞了兩個參數:符號( :all )和哈希( :origin=>[zipcode], :within=>10 )。 當只需要一個參數時,它將接收兩個參數,從而給您錯誤:

wrong number of arguments (2 for 1)  

有兩種解決方法。

首先,您可以刪除:all符號並使用finder方法:

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

或者,你可以完全忘記GeoKit和使用地理編碼器來代替。 (github)

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

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

暫無
暫無

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

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