2
私は、これらの二つのモデルRuby on Rails 3でネストされたフォームのオブジェクトのメソッドを呼び出す方法は? (!)
class Invoice < ActiveRecord::Base
has_many :items
accepts_nested_attributes_for :items
...
end
class Item < ActiveRecord::Base
belongs_to :invoice
def total
price * quantity
end
...
end
と、このネストされた形持っている両方のモデルへ投稿:
<h1>Add an Invoice</h1>
<%= form_for @invoice do |f| %>
<p>
<%= f.label :recipient %>
<%= f.text_field :recipient %> </p>
<p>
<%= f.label :date %>
<%= f.text_area :date %>
</p>
<h2>Items</h2>
<p>
<%= f.fields_for(:items) do |f| %>
<%= f.label :description %>
<%= f.text_field :description %>
<%= f.label :price %>
<%= f.text_field :price %>
<%= f.label :quantity %>
<%= f.text_field :quantity %>
<%= f.label :total %>
<%= f.total %><!-- this method call is not working! -->
<% end %>
</p>
<%= f.submit %>
<% end %>
どのように私は、フォーム内の私の項目に対して計算を行うことができますか?しかし、フォームで、私はそれがf.total
で動作するように取得することはできません
def total
price * quantity
end
:私はこの方法を持っている私のItems
モデルで
。このエラーが発生し続ける:
undefined method `total' for #<ActionView::Helpers::FormBuilder:0x10ec05558>
私はここで何が欠けていますか?
で
を交換する必要があり、アイテムを呼び出します。どうもありがとう!これらの入れ子になったフォームはかなり難しいです... – Tintin81