2011-12-13 45 views
0

在我的控制器:Rails的:「你有一個零對象時,你沒想到吧」

Article.author_ids_in(params[:filters][:author_ids]) 

這當然返回如果這些錯誤(「你有一個無對象...」)特定的參數尚未通過。下面是該模型的方法:

def self.author_ids_in(filters) 
    unless filters.nil? 
    where(:author_id + filters) 
    else 
    scoped 
    end 
end 

正如你所看到的,我已經爲nil對象準備,所以我怎麼能得到紅寶石允許零傳遞?

+0

你確定你的'params [:filters]'不是'nil'嗎?請給出完整的錯誤信息。 –

回答

3

PARAMS [:過濾器]最有可能是零所以它拋出一個異常,當你試圖去把它拿到[:author_ids]

要麼檢查您撥打的範圍之前,或搶救除外:

begin 
    Article.author_ids_in(params[:filters][:author_ids]) 
rescue NoMethodError 
    # do something else here 
end 
+0

賓果。謝謝你,先生。 – imderek

相關問題