我已经在轨道嵌套表格视图被称为像这样问题在嵌套形式局部存取变量
<% f.fields_for :invoice_item_numbers do |item_no_form| %>
<%= render 'invoice_item_number', :f => item_no_form %>
<% end %>
和部分(_invoice_item_number.html.erb)看起来像这样
<div class='invoice_item_numbers'>
<% if f.object.new_record? %>
<li><%= f.label :item_number %><%= f.text_field :item_number %>
<%= link_to_function "remove", "$(this).parent().remove()", :class => 'remove_link' %></li>
<% else %>
<li class="inline"><%= f.label :item_number %><%= f.text_field :item_number %>
</li><li class="inline"><%= f.label :description %><%= invoice_item_number.description %></li><li><%= f.label :amount %><%= f.text_field :amount %>
<%= f.check_box '_destroy', :class => 'remove_checkbox' %>
<%= f.label '_destroy', 'remove', :class => 'remove_label' %></li>
<% end %>
</div>
这种失败,出现错误消息
undefined method `description' for nil:NilClass
为什么invoice_item_number在这部分返回nil对象?这显然是被莫名其妙地定义,因为如果我将其更改为别的东西(如item_number.description
则错误消息成为ITEM_NUMBER undefined local variable or method
”为#instead. The invoice_item_number object that is being displayed by this partial is being used perfectly well by the form helpers as
<%= f.text_field:%ITEM_NUMBER>and
<%f.text_field:数量%>both work perfectly well. I have tried a number of solutions such as using
@ invoice_item_number`和显式定义的渲染方法的对象,但这些都没有奏效
大概有一个很简单的答案,这
甜。 (我误解了你的问题,对不起;-) – ohho 2010-04-23 09:21:48
没问题。我很感激你花时间阅读它。谢谢。 – brad 2010-04-23 10:43:50
是我还是大多数名为Ryan的'着名'rails开发人员? Ryan Bigg,Ryan Bates,Ryan Daigle .. wtf hahah – imjp 2011-07-29 13:20:51