简体   繁体   English

NoMethodError(nil的未定义方法`[]':NilClass)

[英]NoMethodError (undefined method `[]' for nil:NilClass)

I have a very odd instance of this error: 我有一个非常奇怪的错误实例:

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'

Whats weird is that the line 150, where it says the error is, is inside a loop and executes perfectly 11 times before it decides to error out. 奇怪的是,它所说错误的第150行是在一个循环中并在它决定错误之前完美地执行了11次。 I am out of ideas as to why it would work fine but fail one line before what would effective be the loop where the if statement returns true. 我不知道为什么它会正常工作但是在if语句返回true的循环之前失败了一行。

This is the code: 这是代码:

  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

And this is the output before I get the error: 这是我收到错误之前的输出:

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

Thanks in advance for any help. 在此先感谢您的帮助。

One of your @fields elements doesnt contain Title in :field_data. 你的一个@fields元素不包含Title in:field_data。 Try inspecting @fields before calling @fields.each : 在调用@fields.each之前尝试检查@fields.each

Rails.logger.warn '-'*40
Rails.logger.warn @fields.inspect

Check the server logs to see what elements you have in @fields. 检查服务器日志以查看@fields中的元素。

For that error, see also: http://mongoid.org/en/mongoid/docs/tips.html 对于该错误,另请参阅: http//mongoid.org/en/mongoid/docs/tips.html

eg maybe you're using MongoID and an older version of Ruby. 例如 ,你可能正在使用MongoID和旧版本的Ruby。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM