【问题标题】:You have a nil object when you didn't expect it! You might have expected an instance of Array当你没想到时,你有一个 nil 对象!您可能期望有一个 Array 的实例
【发布时间】:2012-03-09 17:54:43
【问题描述】:

我是这样写的

def search

   cuisine=Settings.allcuisines

 begin 
 @orgs = getSearch(params[:lat], params[:lon],params[:zip], params[:dist],   cuisine,params[:num_results])
  respond_to do |format|
    format.html {render action: "index"}
    format.json { render json: @orgs }
  end
    messg="success"
    code="0"
    results={:message=>messg,:code=>code}
    resul=Results.new(results)
     resul.save
  rescue Exception => exc
   messg=exc.message
   code="1"
   results={:message=>messg,:code=>code}
   resul=Results.new(results)
     resul.save
   respond_to do |format|
    format.html {render action: "index"}
    format.json { render json: @orgs }
   end

end

我收到错误:

当你没想到时,你有一个 nil 对象!你可能有 期望一个 Array 的实例。评估时发生错误 nil.each

为什么会这样?

【问题讨论】:

  • 实际错误不在该代码中。它要么在它调用的方法中,要么在你的视图中。
  • def getSearch(lat, lon,zip, dist, food, num_results, unit=:mi) num_results ||= 10 unit ||= :mi if zip.strip.empty? org.where('content.attribs.name'=>'cuisine', 'content.attribs.value'=>cuisine ) .geo_near([lon,lat], :max_distance => dist, :spherical => true, num : num_results) end if(lat.strip.empty?|| lon.strip.empty?) Zipcode.where('zipcode'=>zip) 方法如上,视图是
  • 您可以点击问题下方的“编辑”以添加额外信息:)

标签: ruby-on-rails mongodb


【解决方案1】:

在您看来,您可能正在调用@orgs.each,而@orgs 为零。您可以尝试将迭代器嵌套在:

- unless @orgs.nil?
  - @orgs.each do |o|
    blah

【讨论】:

  • 是的,我正在使用这样的视图,
猜你喜欢
相关资源
最近更新 更多
热门标签