1

我在使用 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: 

任何帮助表示赞赏。

4

1 回答 1

0

geo_scope只需要一个参数:哈希。您正在向它传递两个参数:一个符号 ( :all) 和一个哈希 ( :origin=>[zipcode], :within=>10)。当它只需要一个参数时,它会接收两个参数,从而给您错误:

wrong number of arguments (2 for 1)  

有两种方法可以解决这个问题。

首先,您可以删除:all符号并使用查找器方法:

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)
于 2012-12-16T05:17:44.387 回答