为每个生成的字段集创建/新的DB记录
问题描述:
我在保存多条记录时遇到了问题。为每个生成的字段集创建/新的DB记录
该脚本将通过连接表加载属于部门的乐器列表。 这个表格会为另一个连接表创建一个新的记录,问题是当我有4个乐器时它只会保存最后一个乐器。
任何人可以帮助我解决这个问题,或者点我到正确的方向?
<%= 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 %>
答
只保存最后一个,因为这些字段具有相同的名称,最后一个覆盖所有其他值。查看您在接收控制器上获得的参数。
您可能需要/需要将部门配置为accepts_nested_attributes_for :instruments
,并将部门对象的表单配置为fields_for: instruments
。我突出显示了关键字,我可以帮助您获得所需的信息,Rails Guides是一个很好的开始。
谢谢你的信息莱托我要去研究这些作品。 –