访问嵌套形式部分变量时出现问题

发布于 2024-08-30 03:46:22 字数 1602 浏览 7 评论 0原文

我在 Rails 视图中有一个嵌套表单,像这样调用

<% 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

Why does unavailable_item_number return a nil object in thispart?它显然是以某种方式定义的,因为如果我将其更改为其他内容(例如 item_number.description ),则错误消息将变为 未定义的局部变量或方法item_number' for #相反,该部分显示的invoice_item_number对象被表单助手很好地使用为<%= f.text_field :item_number %><% f。 .text_field :amount %>两者都工作得很好。我尝试了多种解决方案,例如使用@invoice_item_number`并在渲染方法中显式定义对象,但这些

可能 都不起作用。对此有一个非常简单的答案。

I have a nested form in a rails view that is called like this

<% f.fields_for :invoice_item_numbers do |item_no_form| %>
    <%= render 'invoice_item_number', :f => item_no_form %>
<% end %>

and the partial (_invoice_item_number.html.erb) looks like this

<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>

This fails with the error message

undefined method `description' for nil:NilClass

Why does invoice_item_number return a nil object in this partial? It is obviously being defined somehow because if I change it to something else (e.g. item_number.description then the error message becomes undefined local variable or methoditem_number' for #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 :amount %>both work perfectly well. I have tried a number of solutions such as using@invoice_item_number` and explicitly defining an object in the render method but these have not worked.

Presumably there is a very simple answer to this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

扛起拖把扫天下 2024-09-06 03:46:22

只需重新阅读 Ryan 关于嵌套表单的剪贴帖,网址为 http://ryandaigle.com/articles/2009/2/1/what-s-new-in-edge-rails-nested-attributes并找到了答案。它已经在我的代码中了,但我不明白发生了什么。我可以使用 f.object 访问发票_item_number 对象,因此用 <%= f.object.description %> 替换 <%= Invoice_item_number.description %> 解决了我的问题。

Just re-read the Ryan's scraps post on nested forms at http://ryandaigle.com/articles/2009/2/1/what-s-new-in-edge-rails-nested-attributes and found the answer. It was sort of in my code already but I didn't understand what was happening. I can access the invoice_item_number object with f.object so replacing <%= invoice_item_number.description %> with <%= f.object.description %> solved my problem.

无远思近则忧 2024-09-06 03:46:22

如何将: 更改

<%= invoice_item_number.description %>

<%= f.label :description %>

或如果您需要一个字段:

<%= f.text_field :description %>

how about changing:

<%= invoice_item_number.description %>

to

<%= f.label :description %>

or if you need a field:

<%= f.text_field :description %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文