访问嵌套形式部分变量时出现问题
我在 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 method
item_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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需重新阅读 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.如何将: 更改
为
或如果您需要一个字段:
how about changing:
to
or if you need a field: