0

所以我有一个实体表单,它接受另一个实体的嵌套属性。这表现得非常好。但是,嵌套表单并没有像我希望的那样运行。见下文:

<table class="datagrid">
  <thead class="datagrid">
        <th width="300" align="left">&nbsp;Item</th>
        <% unless current_user.is_partner? %>
            <th width="100">&nbsp;Location</th>
        <% end %>
        <th>&nbsp;Amount Requested</th>
        <th>&nbsp;Amount Checked Out</th>
  </thead>

    <% session[:clicked_request].items.each do |item| %>  
      <tr class="<%= cycle('dg_list_line_odd', 'dg_list_line_even') %>">  
        <td>&nbsp;<%= link_to "#{item.name}", item_path(item) %></td>  
            <% unless current_user.is_partner? %>
            <td>&nbsp;&nbsp;<%= item.location.name %></td>
            <% end %>
            <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<%= item.requested %></td>
            <td><%= f.text_field :amount, :value => item.requested, :size => 1 %></td>
      </tr>  
    <% end %>
</table>
<p>&nbsp;</p>

如您所见,这里有一个“每个”循环,它允许显示并希望创建多个项目。但是,当我按下提交按钮时,无论存在多少项目,都只会创建其中一个。

非常感谢您的建议。

4

1 回答 1

0

这不是你渲染表单的方式accepts_nested_attributes

假设f是绑定到父对象的表单构建器,您需要执行类似的操作

<%= f.fields_for :items do |item_form| %>
  <%= item_form.text_field :amount
<% end %>

该块将对集合中的每个项目执行一次。此外,item_form设置了正确的前缀,以便控制器端正常工作。

于 2012-06-24T18:58:39.783 回答