- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试图一次为同一个模型创建多个记录(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>
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"}
<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/
这是一个新手理论问题 - 我刚刚开始使用 Python 并研究 Django 和 orm。问题:如果我开发我的对象并通过额外的开发修改基础对象结构、继承等 - Django 的 ORM 解决方案会自动
我正在使用带有服务器端处理器的 JavaScript 表单,并且我希望能够让表单根据下拉列表转到不同的电子邮件。我已经根据其他表格尽了最大努力,但似乎无法通过电子邮件。我已在电子邮件地址的选项标签下添
一个简单的问题:给定定义,(来自 Haskell SOE) do x — el; el\ ...; en => el »= \x — do e2\ ...; en 和: do let d
我是 Angular 5 的新手。我目前正在研究 Angular Reactive 表单。我有一个下面的 JSON 结构,我需要在从 FORM 获取值后发回 REST API。 JSON 结构: {
我是 Angular 5 的新手。我目前正在研究 Angular Reactive 表单。我有一个下面的 JSON 结构,我需要在从 FORM 获取值后发回 REST API。 JSON 结构: {
我有一个类型(称之为 A),我想创建一个 A -> A、A -> A -> A、A -> A -> A -> ... 等类型的函数的类型类.这不起作用: {-# LANGUAGE FlexibleIn
我正在使用 java 线程同时管理多个 (3) 程序。1 用于 Java swing 表单(绘制 UI 以进行输入),1 用于在系统托盘上设置图标(从 UI 获取输入后立即启动),1 用于处理输入并将
在当前的元素中,我在表单中遇到了一个问题。表单中标签的字体大小可能大于默认值。如果我把它举起来,那么右边的输入必须垂直居中。 我查看了 Bootstrap 和 Foundation,但都没有解决这个问
为了好玩,我使用了一段从 friend 那里得到的代码,并尝试创建一个包含用户名和密码的登录字段,但我很难获得单词旁边的字段。 username 这个词和你输入的框之间有很大的差距。密码也是如此。 这
我的表单中有一个嵌套的控制组,我想访问它们的表单状态值(如原始和有效)以动态显示验证错误。 是这样动态构建的 controlMap['password'] = this.password; contr
发送后我试图重置我的表单,但只有值设置为空。 component.html {{note.value?.length || 0}}/10
我正在尝试自定义 Stripe 结帐表单,但我不知道如何添加输入。我想添加“电话号码”和“姓名”以创建费用和客户。你知道我该怎么做吗? 这是我应该自定义的代码。 最佳答案 您将无法使用
所以我有这个需求,我想以表格的形式提交一个由五个记录组成的表单。这就是它的样子表: 这是对应的代码: Section Q.No Question
我有一个使用 react 形式和输入文本的情况。 我需要: 当用户输入时,根据输入的内容建议一个列表(我使用的是 ngx bootstrap typeahead); 仅当用户失去输入焦点时才验证输入字
我希望重构我的 Angular 项目中的大量组件,以具有强类型的 FormGroups、FormArrays 和 FormControls。 我只是在寻找一种实现强类型 react 形式的好方法。任何
我有事件表格: 'horizontal', 'fieldConfig' => [ 'template' => "{input}\n{hint}\n{error}",
是否有关于如何实现多选和响应式表单的示例? 我正在尝试在 multiselect-dropdown 上设置所选项目(从数据库中检索),它会更新显示的项目( View ),但会引发以下错误: core.
我想在表单中添加按钮以动态添加输入。但是我发现,如果我在表单中添加了一个仅记录到控制台的按钮(并且当我尝试添加输入时),它将记录日志,然后表单中断。我的Electron应用程序的前端窗口崩溃(不退出但
我有一个这样的表格 此表单位于指令内: angular.module('crowdcoreApp').directive('investorForm',function(){
我在 angularjs Controller 中调用的 $mdDialog 中有一个表单,如下所示: actions-controller.js function callForm() {
我是一名优秀的程序员,十分优秀!