我已經在軌道嵌套表格視圖被稱爲像這樣問題在嵌套形式局部存取變量
<% 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