@userrole = Userrole.where('userID=?',@user.id)
@userrole.each do|userrole|
puts userrole.roleID
@role = Role.where('id=?',userrole.roleID)[0]
@cfunction = AuthCf.where('authID=?',@role.authorityID)
@cfunction.each do|cfunction|
puts cfunction.CFID
@function = CFf.where('cFID=?',cfunction.CFID)
@funciton.each do|function|
@f = Function.where('id=?',function.fID)[0]
@arr << @f.name
end
end
end
@funciton.each do|function|
报错
undefined method ''each' for nil:NilClass
因为没有找到呀,还有 ror 里面的字段最好不要用大写。 ( @userrole||[]).each do|userrole| puts userrole.roleID @role = Role.where('id=?',userrole.roleID)[0] @cfunction = AuthCf.where('authID=?',@role.authorityID) (@cfunction || []).each do|cfunction| puts cfunction.CFID @function = CFf.where('cFID=?',cfunction.CFID) (@funciton || []).each do|function| @f = Function.where('id=?',function.fID)[0] @arr << @f.name end end end
@zj0713001 呵呵,恩是的,我只是想告诉他如果要是报错是 nil 的情况进行循环可以用那样的方法。哇哈哈。而且他现在是用的 where,如果要是用个 find_by_xxxx 呢
@zj0713001 不过我觉得女神的那个 to_a 一下很影响效率耶。如果要是是数组的话,那个数组也会再次的 to_a 一下哦!女神不是吃饱了没事干么!
一开始看得我一头雾水啊 @function.each do |function|
太难理解了 -_-|| @functions.each do |function|
才是约定吧?
#20 楼 @zj0713001 params 我明白,楼主那个不是自己从数据库里面抓出来的嘛
另外想请教一下 params 转 to_s 在 rails 4 里头应该怎么写.. 还是就不用了?
#21 楼 @blacktulip 请教错人了外国银... 我还没正式写过 rails4 呢... 但是从 strong_parameters 的介绍里来看 the key :id will pass the whitelisting if it appears in params and it has a permitted scalar value associated. Otherwise the key is going to be filtered out, so arrays, hashes, or any other objects cannot be injected. The permitted scalar types are String, Symbol, NilClass, Numeric, TrueClass, FalseClass, Date, Time, DateTime, StringIO, IO, ActionDispatch::Http::UploadedFile and Rack::Test::UploadedFile.
也就是说 只是过滤了而已 想 to_s 应该还是没问题的... 比方说 Date to String~