gpt4 book ai didi

ruby-on-rails - Rails 4 以一种形式创建同一模型的多个记录

转载 作者:行者123 更新时间:2023-12-04 05:46:36 25 4
gpt4 key购买 nike

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

基本上,我的索引 View 中有一个表单,只需单击 + 号(使用 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>

这是我的 Controller 中的内容(一项工作有很多设备):
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"}

最佳答案

唯一的问题是在你的 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到你的表格。

关于ruby-on-rails - Rails 4 以一种形式创建同一模型的多个记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25126481/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com