繁体   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