简体   繁体   中英

Ruby on Rails: GeoKit Zipcode Search?

I am having problems getting a zip code search to work with 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. You are passing it two arguments: a symbol ( :all ) and a hash ( :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:

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

Or, you could forget GeoKit altogether and use geocoder instead. (github)

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM