Ruby:nil:NilClass的未定义方法`[] ='

用户名

对不起,我的英语不好。我收到如下错误。

undefined method `[]=' for nil:NilClass

app/controllers/tasks_controller.rb:69:in `block in show'
app/controllers/tasks_controller.rb:67:in `each'
app/controllers/tasks_controller.rb:67:in `each_with_index'
app/controllers/tasks_controller.rb:67:in `show'

这是“ tasks_controller.rb”中的代码:

@teammates_form = Array.new(@teammates.size-1){Hash.new}
@teammates.each_with_index do |t, idx|
  if t.id != current_user.id
    @teammates_form[idx]['id'] = t.id  # <--- line 69
    @teammates_form[idx]['name'] = t.name
    @teammates_form[idx]['t_id'] = @task.id
  end
end

但是,如果我更改代码,例如:

@teammates_form[0]['id'] = t.id
@teammates_form[0]['name'] = t.name
@teammates_form[0]['t_id'] = @task.id

它的工作原理...我不知道为什么不能在数组中使用索引。

在此先感谢您的帮助。

阿列克谢·马蒂什金(Alexei Matiushkin)
#                                          ⇓⇓  ? WHY?
@teammates_form = Array.new(@teammates.size-1){Hash.new}

您会在的最后一次迭代中产生此错误@teammates,因为@teammates_form它缩短了1个元素。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章