[英]Ruby on Rails: GeoKit Zipcode Search?
I am having problems getting a zip code search to work with GeoKit. 我在获取邮政编码搜索以与GeoKit配合使用时遇到问题。 Some error is making the whole app crash.
某些错误导致整个应用崩溃。
This is what I have: 这就是我所拥有的:
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"
This is the error I am getting: 这是我得到的错误:
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:
Any help appreciated. 任何帮助表示赞赏。
geo_scope
expects only one argument : a hash. geo_scope
只需要一个参数 :哈希。 You are passing it two arguments: a symbol ( :all
) and a hash ( :origin=>[zipcode], :within=>10
). 您为其传递了两个参数:符号(
:all
)和哈希( :origin=>[zipcode], :within=>10
)。 It is receiving two arguments when it expects only one, giving you the error: 当只需要一个参数时,它将接收两个参数,从而给您错误:
wrong number of arguments (2 for 1)
There are two ways to solve this. 有两种解决方法。
First, you can remove the :all
symbol and use a finder method: 首先,您可以删除
:all
符号并使用finder方法:
Bathroom.geo_scope(:origin=>[zipcode], :within=>10).all
Or, you could forget GeoKit altogether and use geocoder instead. 或者,你可以完全忘记GeoKit和使用地理编码器来代替。 (github)
(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.