請求書フォームの複数のアイテムの在庫または数量をオンザフライで変更したいと考えています。基本的には、これは、他の商品の一部である特定の商品の在庫に影響を及ぼすためです。例えば、他の商品を含むギフトバスケットです。フォームビューで配列IDと名前を作成しない、ネストされたhas_many関連のレンダリング
これは私の質問ではありません。私がでてる説明する目的のために、ここに行く:
1)アイテム
2) item_relationshipsはフィールドが含まれてitem_relationships
テーブル:、ITEM_ID:linked_item_id、:item_ratioを、:linked_item_ratioを、:changes_inventory
モデル
# Item.rb
has_many :relationships, :class_name => 'ItemRelationship', :dependent => :destroy
accepts_nested_attributes_for :relationships
# ItemRelationship.rb
belongs_to :item
<%
disable_hash = relationship.new_record? ? {:disabled => true} : {}
%>
<%= f.fields_for @item.relationships, relationship do |r| %>
<div<%= raw(" style=\"display:none\"") unless disable_hash.empty? %>>
<%= link_to_function "X", "remove_relationship(this)", :style => "font-style:italic", :tabindex => -1 %>
For Every <%= r.select :item_ratio, 1..10, {}, disable_hash %> of this item,
add <%= r.select :linked_item_ratio, 1..10, {}, disable_hash %>
to the <%= r.select :changes_inventory, [["Inventory", true], ["Quantity", false]], {}, disable_hash %>
of <%= r.text_field :linked_item_id, :size => 20, :title => "Use a Product's Barcode Here", disable_hash.key(0) => disable_hash[:disabled] %>
</div>
<% end %>
# Relationships can be inserted into the form, so I create one which doesn't have
# an ID so I can use it as a template to copy/insert via javascript. The next block enumerates over existing relationships
<%= render :partial => "item_relationship_form", :locals => {:f => f, :relationship => @item.relationships.new} %>
<% @item.relationships.collect{|rel| rel unless rel.id.nil?}.compact.each do |r| %>
<%= render :partial => "item_relationship_form", :locals => {:f => f, :relationship => r} %>
<% end %>
一部ビューは、今私の問題に1人のだけの関係があったかのように、フォームは、各要素のIDと名前を生成します。どこでも "[]"の括弧はありません。フォームが生成される前に手動で関係のIDを設定すると、通常は見つかるタグ属性に数値が生成されません。
EX)
item[item_relationship][item_ratio]
代わりに与えるもの
item[item_relationships][][item_ratio]
?