明らかな何かが欠けているかもしれませんが(うまくいけば)、ネストされた形式でレコードを保存するという奇妙な問題が発生しています。これはかなり基本的な設定ですが、私のLineItemモデルが2語の関係(:line_items)であるという点で少し複雑です。ただし、Railsのガイドラインに従っており、正常に機能しているようです。
フィクスチャはline_itemsと請求書の間に適切な関係を作成しており、すべてがビューに正しく表示されていますが、(Railsコンソールまたはビューで)正しく保存するline_itemレコードを取得できません。
class Invoice < ActiveRecord::Base
attr_accessible :line_items #and the rest of my relevant attributes
has_many :line_items, :dependent => :destroy
accepts_nested_attributes_for :line_items, :allow_destroy => true
# Rest of my model code
end
class LineItem < ActiveRecord::Base
attr_accessible :invoice_id #and the rest of my relevant attributes
belongs_to :invoice
end
このline_items_attributes=
メソッドは私の請求書には存在しますが、新しい請求書のline_itemsは保存されません。さらに苛立たしいことに、既存のline_itemsを編集したり、事後に割り当てることはできますが、一挙に行うことはできません(ネストされた属性の要点は?)。私のビューでは、請求書フォームから既存のline_itemsを編集することさえできません。何か案は?より多くのコードを投稿できてうれしいですが、簡潔にするために投稿しませんでした。
前もって感謝します...
コードの表示(リクエストによる):
(請求書の一部のフォーム)
<%= form_for(@invoice) do |f| %>
<% @invoice.line_items.build unless @invoice.line_items.any? %>
...
<% f.fields_for :line_items do |builder| %>
<%= render 'line_item_fields', :f => builder %>
<% end %>
(ラインアイテムのフォームパーシャル)
...
<%= f.collection_select :sku_id, @skus, :id, :name, :prompt => true %>
<%= f.hidden_field(:_destroy) + link_to_function(name, "remove_fields(this)") %>
(javascript)
function remove_fields(link) {
$(link).previous("input[type=hidden]").value = "1";
$(link).up(".fields").hide();
}