2011-02-04 4 views
0

入れ子になっている2つのフォームを作成しようとしています。しかし、私が見つけたすべてのリソースは、メインオブジェクトを保存する方法、ネストされたオブジェクトを追加または削除する方法がありますが、フォームの値を使ってリスト全体を保存することはできません。これを行う方法はありますか?すべての入れ子になったフォームオブジェクトをまとめて保存する

私は一緒に働いているモデルです。私はあなたが異なるコンポーネントと数量を選択することができるPCのコンフィギュレータを作成しようとしているし、あなたはバスケットに追加します。

私は3つのモデルで作業しています。 Configuration, Configoption, Item

- 構成モデル

has_many :configoptions 

- Configoptionモデル

belongs_to :configuration 
    has_many :items 

    def range 
    a = [] 
    b = self.quantity_rng.scan(/\w/) 
    b.each do |p| 
     a << p.to_i 
    end 
    return a 
    end 

- アイテムモデル

belongs_to :configoption 
    scope :sorted, order('items.position ASC') 

はアイデアが言う、すべての設定は、オプションのセットを持っているということです5.これらの5つのオプションのそれぞれには、いくつかの利用可能な項目があります。例えば、オプション1はプロセッサ、2はRAMなどである。

- 設定コントローラ

def list 
    @configurations = Configuration.find(:all) 
    end 

    def show 
    @configuration = Configuration.find(params[:id]) 
    @options = @configuration.configoptions 
    end 

- これまでのところ、物事が実際に良いです設定/ショービュー

<table> 
<tr> 
    <th>Elements</th> 
    <th>Quantity</th> 
</tr> 

    <%= form_for(:configuration, :remote => true) do |p| %> 
     <% @options.each do |option| %> 
     <tr> 
     <%= form_for(:option, :remote => true) do |f| %> 
      <% if option.items.count > 1 %> 
      <th id="<%= option.name %>"> <%= f.select option.name, options_from_collection_for_select(option.items.sorted, 'id', 'name')%></th> 
      <% else%> 
      <th id="<%= option.name %>"> <%= f.label(option.items[0].name) %></th> 
      <% end %> 
      <% if option.quantity_rng == nil %> 
      <th id="<%= option.name + "_qty" %>"> <%= f.label(option.quantity) %></th> 
      <% else%> 
      <th id="<%= option.name %>"> <%= f.select option.name, option.range, :selected => option.quantity%></th> 
      <% end %> 
     <% end %> 
     <% end %> 
    </tr> 
    <% end %> 

</table> 

、私はオプションに異なるアイテムを与えることができると.range方法は、さんに私を聞かせてオプション1,2,3でquantity_rngと言って、必要に応じてドロップダウンのために渡された配列[1,2,3]に変換します。

しかし、今では私が選んだものを実際にバスケットに追加する重要な部分があります。ユーザーがドロップダウンで操作した内容をキャプチャして保存するにはどうすればいいですか?

BTW私はremote => trueを使用しています。 jQueryの検証ルールは後でありますが、一度に1ステップです)

ありがとうございます!

答えて

0

あなたのアイデアはよく理解できますが、ネストされたオプションとしてp.fields_forを使用する必要があります。 http://apidock.com/rails/v3.0.0/ActionView/Helpers/FormHelper/fields_forを見て、あなたのモデル構成に

def address_attributes=(attributes) 
    # Process the attributes hash 
end 

を定義することを忘れドット

関連する問題