Ruby on rails 控制器随机返回错误
RubyonRails3 在我的控制器中,index()是我定义的实例变量。昨天这个很好。今天我遇到了这个错误:Ruby on rails 控制器随机返回错误,ruby-on-rails,ruby-on-rails-3,loops,if-statement,Ruby On Rails,Ruby On Rails 3,Loops,If Statement,RubyonRails3 在我的控制器中,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/
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
同样的错误,有什么建议吗?谢谢看起来您有一个没有用户id的证书记录。请删除该记录,或者最好向证书模型添加一些验证,以确保填充此字段
或者,如果将if行更改为
if(User.find(@id))
看起来您有一个没有用户id的证书记录。这对模型的工作方式有效吗?如果将if行更改为if(User.find(@id)),则第二个代码块将工作
你是对的。我毁了它,一切都很好。谢谢作为答案张贴。