Ruby on rails 访问嵌套形式的变量时出现问题

Ruby on rails 访问嵌套形式的变量时出现问题,ruby-on-rails,forms,partial-views,nested-forms,Ruby On Rails,Forms,Partial Views,Nested Forms,我在rails视图中有一个嵌套表单,它被称为 <% f.fields_for :invoice_item_numbers do |item_no_form| %> <%= render 'invoice_item_number', :f => item_no_form %> <% end %> <div class='invoice_item_numbers'> <% if f.object.new_record? %>

我在rails视图中有一个嵌套表单,它被称为

<% f.fields_for :invoice_item_numbers do |item_no_form| %>
    <%= render 'invoice_item_number', :f => item_no_form %>
<% end %>
<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>
为什么发票\项目\编号在此部分中返回一个nil对象?很明显,它是以某种方式被定义的,因为如果我把它改成别的东西(例如,
item_number.description
则错误消息变为#
未定义的局部变量或方法
item_number'。此部分显示的发票_item_number对象正被表单帮助程序很好地使用,因为
这两种方法都很好地工作。我尝试了num许多解决方案,例如使用
@invoice\u item\u number`并在渲染方法中显式定义对象,但这些方法都不起作用

大概有一个非常简单的答案。

改变一下怎么样:

<%= invoice_item_number.description %>


或者,如果您需要字段:

<%= f.text_field :description %>


只要重新阅读Ryan在嵌套表单上发布的帖子,就找到了答案。它已经在我的代码中了,但我不知道发生了什么。我可以用f.object访问invoice\u item\u number对象,因此将
替换为
解决了我的问题。

好吧,我已经在那里有了f.label:description.Int通常,如果我输入f.text\u field:description,这会完全按照我的要求访问description方法,但我不需要文本字段,我只需要文本。谢谢。尽管如此。发票\u项目\u编号为零(尚未定义)在部分中。如果要访问模型数据,需要在控制器中定义@invoice\u item\u number并通过@invoice\u item\u number.descriptionHmm进行访问,但在这种情况下,invoice\u item\u number是一个嵌套属性。父属性(invoice)在控制器中定义为@invoice=invoice.find(参数[:id])。我真正想访问的是invoice.invoice\u item\u number.description,但我不确定如何在我的分部中访问。您可以将多个参数传递给分部,语法为:'invoice\u item\u number',:locals=>{:f=>item\u no\u form,:x=>123}%%然后您可以在部分中使用x作为局部变量。没问题。我很感谢您花时间阅读。谢谢。是我还是大多数名为Ryan的“著名”rails开发人员?Ryan Bigg、Ryan Bates、Ryan Daigle..哈哈哈
<%= f.label :description %>
<%= f.text_field :description %>