ruby-on-rails – Rails 4在一个表单中创建相同模型的多个记录

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – 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","date_in"=>"","date_out"=>"","technician_return"=>"frank"}],"job_id"=>"3"}

解决方法

唯一的问题是你的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.

原文链接:https://www.f2er.com/ruby/268034.html

猜你在找的Ruby相关文章