Rails 4在一个表单中创建相同模型的多个记录

cod*_*ear 6 ruby-on-rails ruby-on-rails-4

我试图一次为同一个模型创建多个记录(Rails 4.1.2),但表单只发送一组参数值(实际上只是第一行)...

基本上我在索引视图中有一个表单,只需单击+符号(使用javascript附加表行)添加更多记录,一次添加更多行记录...并点击提交一次创建所有记录.

以下是我到目前为止的观点:

 <div class="table-responsive">
   <table class="table equipment-table" id="EquipmentTable">
     <thead>
       <tr>
         <th>Equipment name</th>
         <th>Serial number</th>
         <th>Date out warehouse</th>
         <th>Date in warehouse</th>
         <th>Technician</th>
         <th>Site location</th>
         <th>Date in</th>
         <th>Date out</th>
         <th>Technician return</th>
       </tr>
     </thead>

     <tbody id="tableToModify">
       <% @equipments.each do |equipment| %>
       <tr>
         <td><%= equipment.equipment_name %></td>
         <td><%= equipment.serial_number %></td>
         <td><%= equipment.date_out_warehouse %></td>
         <td><%= equipment.date_in_warehouse %></td>
         <td><%= equipment.technician %></td>
         <td><%= equipment.site_location %></td>
         <td><%= equipment.date_in %></td>
         <td><%= equipment.date_out %></td>
         <td><%= equipment.technician_return %></td>
       </tr>
       <% end %>

       <%= form_tag make_multiple_equipments_path, method: :post do %>

          <tr class="equipment_row" id="rowToClone">
          <%= fields_for "equipments[]", @equipment do |f| %>
            <td><%= f.text_field :equipment_name, size: 15 %></td>
            <td><%= f.text_field :serial_number, size: 7 %></td>
            <td><%= f.date_field :date_out_warehouse %></td>
            <td><%= f.date_field :date_in_warehouse %></td>
            <td><%= f.text_field :technician, size: 12 %></td>
            <td><%= f.text_field :site_location, size: 12 %></td>
            <td><%= f.date_field :date_in %></td>
            <td><%= f.date_field :date_out %></td>
            <td><%= f.text_field :technician_return, size: 15 %></td>
            <td><input type="button" id="delbutton" class="btn btn-danger" value=" - " onclick="deleteRow(this)"/></td>
          <% end %>
          </tr>

          </tbody>
          </table>

       <input type="button" onclick="cloneRow()" value=" + " class="btn btn-success"/>  
       <br>
       <%= submit_tag "Submit", class: "btn btn-primary" %>
       <% end %>
 </div>


 <script type="text/javascript">
    function deleteRow(row)
    {
      var i=row.parentNode.parentNode.rowIndex;
      document.getElementById('EquipmentTable').deleteRow(i);
    }
    function cloneRow()
    {
      var row = document.getElementById("rowToClone"); // find row to copy
      var table = document.getElementById("tableToModify"); // find table to append to
      var clone = row.cloneNode(true); // copy children too
      table.appendChild(clone); // add new row to end of table
    }
 </script>
Run Code Online (Sandbox Code Playgroud)

这是我在我的控制器中所拥有的(一个工作有很多设备):

def index
  @job = Job.find(params[:job_id])
  @equipments = @job.equipments.all
  @equipment = Equipment.new(:date_out_warehouse => Time.now, :date_in_warehouse => Time.now, :date_in => Time.now, :date_out => Time.now)
end

def make_multiple_equipments
  @job = Job.find(params[:job_id])
  count = 0
  equipments_array = params.permit(equipments: [:equipment_name, :serial_number, :date_out_warehouse, :date_in_warehouse, :technician, :site_location, :date_in, :date_out, :technician_return]).require(:equipments)

  while count < equipments_array.count
    @job.equipments.create(equipments_array[count])
    count = count + 1
  end

  redirect_to job_equipment_index_path(@job), :notice => 'Equipment were successfully created.'
end
Run Code Online (Sandbox Code Playgroud)

这些是我的参数:

{"utf8"=>"?",
"authenticity_token"=>"TdHJAFI+Vw5zxFKDNXbx7LHPcT2mJ4BBg04Qt0Z9QNY=",
"equipments"=>[{"equipment_name"=>"tester",
"serial_number"=>"29",
"date_out_warehouse"=>"2014-08-04",
"date_in_warehouse"=>"2014-08-04",
"technician"=>"paul",
"site_location"=>"steve",
"date_in"=>"2014-08-04",
"date_out"=>"2014-08-04",
"technician_return"=>"mark"}],
"commit"=>"Submit",
"job_id"=>"2"}
Run Code Online (Sandbox Code Playgroud)

如果任何人能够为我提供一些见解,我将不胜感激......

更新1: 我似乎认为问题是在表单中,因为它没有发送一个包含多个项目的数组我更改了下面的表单但仍然在参数中它只发送一组值:

<td><%= text_field_tag "equipments[][equipment_name]" %></td>
<td><%= text_field_tag "equipments[][serial_number]" %></td>
<td><%= date_field_tag "equipments[][date_out_warehouse]" %></td>
<td><%= date_field_tag "equipments[][date_in_warehouse]" %></td>
<td><%= text_field_tag "equipments[][technician]" %></td>
<td><%= text_field_tag "equipments[][site_location]" %></td>
<td><%= date_field_tag "equipments[][date_in]" %></td>
<td><%= date_field_tag "equipments[][date_out]" %></td>
<td><%= text_field_tag "equipments[][technician_return]" %></td>
Run Code Online (Sandbox Code Playgroud)

参数:

 {"utf8"=>"?",
 "authenticity_token"=>"oX6niXZFQQzoomlsojYweAuXtiLjP7LCdKGhOvUe/Vw=",
 "equipments"=>[{"equipment_name"=>"boot",
 "serial_number"=>"23",
 "date_out_warehouse"=>"2014-08-08",
 "date_in_warehouse"=>"2014-08-08",
 "technician"=>"paul",
 "site_location"=>"steve",
 "date_in"=>"",
 "date_out"=>"",
 "technician_return"=>"frank"}],
 "commit"=>"Submit",
 "job_id"=>"3"}
Run Code Online (Sandbox Code Playgroud)

小智 0

唯一的问题是你的 js 克隆函数:

function cloneRow()
{
  var row = document.getElementById("rowToClone"); // find row to copy
  var tableForm = document.getElementById("tableToModify").getElementsByTagName("form"); // find table to append to
  var clone = row.cloneNode(true); // copy children too
  tableForm.appendChild(clone); // add new row to end of table
}
Run Code Online (Sandbox Code Playgroud)

您应该将克隆的行附加到表单中,而不仅仅是在父表中。我收到了表格,但也许您应该考虑在表格中getElementsByTagName添加一个。id