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

时间:2014-08-04 19:55:10

标签: 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>

以下是我控制器中的内容(一项工作有很多设备):

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

这些是我的参数:

{"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"}

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

更新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>

参数:

 {"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"}

1 个答案:

答案 0 :(得分:0)

唯一的问题是你的js clone功能:

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
}

您应该在表单中附加克隆的行,而不仅仅是在父表中。我收到了getElementsByTagName的表单,但您可以考虑在表单中添加id

相关问题