【问题标题】:Rails 4 create multiple records of same model in one formRails 4以一种形式创建同一模型的多个记录
【发布时间】:2014-08-04 19:55:09
【问题描述】:

我正在尝试一次为同一模型创建多个记录(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"}

【问题讨论】:

标签: ruby-on-rails ruby-on-rails-4


【解决方案1】:

唯一的问题在于你的 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
}

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-21
    • 1970-01-01
    • 1970-01-01
    • 2011-02-17
    • 2014-02-27
    相关资源
    最近更新 更多