Ruby on rails rails中nil的未定义方法
我几乎没有rails方面的知识,但是这个项目落到了我的手上,我不得不在其中做一些事情,这不是我的运气吗?它是用rails编写的 所以我看了代码,试图模仿我正在工作的这个页面所做的事情,我认为它是从文件夹或其他东西加载一些pdf到一个div中,不知何故,我不得不做同样的事情,但我不知道如何做 结果给了我这个错误:Ruby on rails rails中nil的未定义方法,ruby-on-rails,Ruby On Rails,我几乎没有rails方面的知识,但是这个项目落到了我的手上,我不得不在其中做一些事情,这不是我的运气吗?它是用rails编写的 所以我看了代码,试图模仿我正在工作的这个页面所做的事情,我认为它是从文件夹或其他东西加载一些pdf到一个div中,不知何故,我不得不做同样的事情,但我不知道如何做 结果给了我这个错误: undefined method `each' for nil:NilClass 及 有什么想法吗 我可以用你需要的东西来更新这篇文章来解决这个问题 编辑: 整个html位: <
undefined method `each' for nil:NilClass
及
有什么想法吗
我可以用你需要的东西来更新这篇文章来解决这个问题
编辑:
整个html位:
<tr>
<td><table width="510" border="0" cellpadding="0" cellspacing="5" bgcolor="#333333">
<tr>
<td><a href="/trofeu_rotax_classificacoes"><span class="style6">CLASSIFICAÇÕES</span></a></td>
<tr>
<td><table width="510" border="0" cellpadding="2" cellspacing="5" bgcolor="#CCCCCC">
<% for trofeu_rotax_classificas in @trofeu_rotax_classifica %>
<tr>
<td width="22" bgcolor="#333333"><div align="center" class="style4">i</div></td>
<td width="377" bgcolor="#E4E4E4"><span class="style18"><%= trofeu_rotax_classificas.titulo %></span></td>
<% unless(trofeu_rotax_classificas.file.blank?) %>
<td width="91" bgcolor="#FF0000"><div align="center"><span class="style5"><%= link_to("download PDF", trofeu_rotax_classificas.file.url, :class => "style5") %></span></div></td>
<% end %>
</tr>
<% end %>
</table></td>
</tr>
</tr>
</table></td>
</tr>
我认为代码还引用了以下文件:
<table>
<tr>
<th class="th_id"><span>Id</span></th>
<th>Titulo</th>
<th class="th_actions"><span>Actions</span></th>
</tr>
<% @trofeu_rotax_classificas.each do |trofeu_rotax_classificas| %>
<tr>
<td class="td_id"><%=h trofeu_rotax_classificas.id %></td>
<td><%=h trofeu_rotax_classificas.titulo %></td>
<td>
<%= link_to image_tag('cms/edit.png'), trofeu_rotax_classificas_path(trofeu_rotax_classificas) %>
<%= link_to image_tag('cms/delete.png'), trofeu_rotax_classificas, :confirm => 'Tem a certeza que deseja apagar este item?', :method => :delete %>
</td>
</tr>
<% end %>
</table>
以及:
而且
/home/korridas/site/app/views/public/trofeu_rotax.html.erb:130:in `_run_erb_app47views47public47trofeu_rotax46html46erb'
<%= render(:layout => "form") %>
而控制器则显示错误
undefined method `each' for nil:NilClass
意味着您正在调用每个nil对象的方法
在您的例子中,变量@trofeu rotax\u classifica似乎为零,因此您希望在您的中检查其赋值。此变量为零:@trofeu rotax\u classifica
Rails中的变量在控制器中设置。在您的服务器日志中,应该显示Started GET for。。。然后用计算机进行处理
无论出于何种原因,该控制器都不会返回变量。实例变量未设置时,默认为nil
看起来你可能有一个公共控制器?如果是这样,问题可能就在那里。在任何地方都看不到每个appear,需要更多关于该错误的代码。我添加的内容是否足够?我们需要查看app/controller.rb文件。如果您可以从项目的根目录开始执行rake路由,这可能有助于查明问题。基本上,在控制器的某个地方,您正在初始化一个名为@trofeu_rotax_classifica的实例变量,这就是您的。。。循环正在逐步执行,但该变量现在初始化不正确或根本没有初始化。。。所以@trofeu_rotax_classifica包含nil和for。。。循环对nil不起作用。如果你没有太多的控制器,也许你可以发布所有控制器?我有45个控制器文件。。。。。我刚刚发布了一个我想我正在做的。这是控制器,我复制了一个我正在模仿的代码,试图改变一些东西,但错误仍然存在
<% form_for(@trofeu_rotax_classificas, :html => { :multipart => true }) do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :titulo %><br />
<%= f.text_field :titulo %>
</p>
<p>
<%= f.label :file %><br />
<%= link_to("Ficheiro", @trofeu_rotax_classificas.file.url) unless(@trofeu_rotax_classificas.file.blank?) %><br />
<%= upload_column_field 'trofeu_rotax_classificas', 'file' %>
</p>
<p>
<%= f.submit 'Create' %>
</p>
<% end %>
<%= render(:layout => "form") %>
undefined method `each' for nil:NilClass