我有一个非常奇怪的错误实例:
NoMethodError (undefined method `[]' for nil:NilClass):
app/controllers/main_controller.rb:150:in `block in find_data_label'
app/controllers/main_controller.rb:149:in `each'
app/controllers/main_controller.rb:149:in `find_data_label'
app/controllers/main_controller.rb:125:in `data_string'
app/controllers/main_controller.rb:35:in `catch'
奇怪的是,错误所在的第 150 行位于一个循环内,并且在它决定出错之前完美地执行了 11 次。我不知道为什么它会正常工作,但在 if 语句返回 true 的循环有效之前失败了一行。
这是代码:
def find_data_label(label)
@fields.each do |f|
puts "f[:field_data]['Title'] = #{f[:field_data]['Title']}" # <--- line 150
if f[:field_data]['Title'] == label
return f
end
end
end
这是我收到错误之前的输出:
f[:field_data]['Title'] = Name
f[:field_data]['Title'] = Name
f[:field_data]['Title'] = Mobile number
f[:field_data]['Title'] = Email
f[:field_data]['Title'] = Date of birth
f[:field_data]['Title'] = Gender
f[:field_data]['Title'] = Street name
f[:field_data]['Title'] = Street number
f[:field_data]['Title'] = My local Puckles store is in
f[:field_data]['Title'] = Suburb
f[:field_data]['Title'] = Postcode
Completed 500 Internal Server Error in 2047ms
提前感谢您的帮助。