【问题标题】:Ruby on Rails: GeoKit Zipcode Search?Ruby on Rails:GeoKit 邮政编码搜索?
【发布时间】:2012-12-16 03:21:43
【问题描述】:

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

任何帮助表示赞赏。

【问题讨论】:

  • 你能包括实际的错误吗?您可以在 Rails 日志 (logs/development.log) 中找到错误/堆栈跟踪
  • ActionController::RoutingError (没有路由匹配 [GET] "/bathrooms/zipcode.json"):
  • 在救援/布局中渲染 /Library/Ruby/Gems/1.8/gems/actionpack-3.2.3/lib/action_dispatch/middleware/templates/rescues/routing_error.erb (2.5ms)
  • 我在路由表中发现了一个错误。但现在我有,“参数数量错误(2 比 1)”
  • 现在,未知的密钥来源......

标签: ruby-on-rails zipcode


【解决方案1】:

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)

【讨论】:

  • 我尝试了 GeoKit 的建议,现在收到此错误:47130 (Array) cannot be normalized to a LatLng.我们尝试将其解释为数组、字符串、Mappable 等,但没有骰子。嗯????
  • 但这有效... @bathrooms = Bathroom.geo_scope(:origin=>params[:zipcode], :within=>10).all
猜你喜欢
  • 1970-01-01
  • 2016-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多