Ruby on Rails:GeoKit Zipcode搜索?

时间:2012-12-16 03:21:44

标签: ruby-on-rails zipcode

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

任何帮助表示感谢。

1 个答案:

答案 0 :(得分: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)