0
Ruby on Rails 3控制器隨機返回錯誤
在我的控制器中index()是我定義的實例變量。昨天這工作得很好。今天,我收到此錯誤:
Couldn't find User without an ID
app/controllers/grandstreamers/resellers_controller.rb:7:in `block in index'
app/controllers/grandstreamers/resellers_controller.rb:5:in `each'
app/controllers/grandstreamers/resellers_controller.rb:5:in `index'
這裏是我的控制器代碼:
4 @certs = Certificate.all
5 @certs.each do |h|
6 @id = h.user_id
7 if @train = (User.find(@id).reseller_id)
8 end
9 end
爲什麼試圖找到沒有ID的用戶?這是一件好事。我希望我的用戶有ID。
如果我刪除循環錯誤消失了。我也試過:
@certs = Certificate.all
@certs.each do |h|
@id = h.user_id
if (User.find(@id).reseller_id)
@train == User.find(@id).reseller_id
end
end
同樣的錯誤,有什麼建議嗎?謝謝
你的第二個代碼塊將工作看起來你有沒有爲user_id證書記錄。這對你的模型應該如何工作有效嗎? – Dhaulagiri
如果您將If行更改爲此if'User.find(@id)' – Dhaulagiri
您的第二個代碼塊將工作您是正確的。我摧毀了它,一切都很好。謝謝!作爲回答發佈。 – DDDD