2016-06-13 97 views
0

我在保存多條記錄時遇到了問題。爲每個生成的字段集創建/新的DB記錄

該腳本將通過連接表加載屬於部門的樂器列表。 這個表格會爲另一個連接表創建一個新的記錄,問題是當我有4個樂器時它只會保存最後一個樂器。

Image generated list

任何人可以幫助我解決這個問題,或者點我到正確的方向?

<%= form_for(:joindaylisting) do |j| %> 
    <% @instrumentslist.each do |instrument| %> 

    <tr class="<%= cycle('odd', 'even') %>"> 
     <td> 
     <% j.label(:instrument_id, "#{instrument.name}") %> 
     <%= link_to("#{instrument.name}", {:controller => 'instruments', :action => 'show_instruction', :instrument_id => instrument.id}, :onclick=>"window.open('height=670, width=675');return false;") %> 
     </td> 
     <%= j.hidden_field(:instrument_id, :value => instrument.id) %> 

     <td></td> 
     <% j.label(:ammountdesinfection, "") %> 
     <td><%= j.text_field(:ammountdesinfection) %></td> 
     <% j.label(:ammountinstruments, "") %> 
     <td><%= j.text_field(:ammountinstruments) %></td> 
     <% j.label(:ammountrelease, "") %> 
     <td><%= j.text_field(:ammountrelease) %></td> 
     <% j.label(:notes, "") %> 
     <td><%= j.text_area(:notes) %></td> 
    </tr> 

    <% j.label(:department_id) %> 
    <%= j.hidden_field(:department_id, :value => @department.id) %> 

    <% end %> 
    <% end %> 

回答

0

只保存最後一個,因爲這些字段具有相同的名稱,最後一個覆蓋所有其他值。查看您在接收控制器上獲得的參數。

您可能需要/需要將部門配置爲accepts_nested_attributes_for :instruments,並將部門對象的表單配置爲fields_for: instruments。我突出顯示了關鍵字,我可以幫助您獲得所需的信息,Rails Guides是一個很好的開始。

+0

謝謝你的信息萊託我要去研究這些作品。 –

相關問題