2012-03-09 43 views
0

我寫的一個實例如下當你沒有想到它時,你有一個零對象!你可能會認爲陣列

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 

我得到錯誤:

You have a nil object when you didn't expect it! You might have expected an instance of Array. The error occurred while evaluating nil.each

這究竟是爲什麼?

+2

實際的錯誤不在該代碼中。無論是在它調用的方法還是在您的視圖中。 – 2012-03-09 17:57:37

+0

def getSearch(lat,lon,zip,dist,cuisine,num_results,unit =:mi) num_results || = 10 unit || =:mi if zip.strip.empty? Org.where('content.attribs.name'=>'cuisine','content.attribs.value'=>烹飪) .geo_near([lon,lat],::max_distance => dist,:spherical => true ,num:num_results) end if(lat.strip.empty?|| lon.strip.empty?) Zipcode.where('zipcode'=> zip) 方法如上,視圖爲<%for org in @機構單位%> ​​<%= org.name%> ​​<%= org.address.city%> ​​<%= org.address.country%> – krishnareddy 2012-03-10 05:45:14

+0

可以單擊下面的 「編輯」你的問題添加額外的信息':)'。 – 2012-03-10 07:19:52

回答

1

您可能在您的視圖中打電話@ orgs.each,@orgs爲零。您可以嘗試將您的迭代器嵌套在:

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

是我使用這樣看來,<%爲組織中@orgs%> ​​<%= org.name%> ​​<%= org.address.city%> ​​<%= org.address.country%> – krishnareddy 2012-03-10 05:42:50