[英]How can I send values of multiple select option fields to a controller
I have a form for a model "Cell" that can be cloned using javascript
.我有一个可以使用javascript
克隆的模型“Cell”的表单。 I have both text fields and select fields in the form that can be cloned.我有可以克隆的表单中的文本字段和选择字段。 Since the cloned element produces an array of Strings, I have sent the text elements as an array by changing the name attribute of the field using the format: cell[ ]['name']
.由于克隆的元素生成一个字符串数组,我通过使用以下格式更改字段的 name 属性将文本元素作为数组发送: cell[ ]['name']
。 However, f.select
field in rails comes with a default name attribute cell[category_id]
.但是, f.select
字段带有默认名称属性cell[category_id]
。 Due to this, I keep getting the error:因此,我不断收到错误消息:
Invalid request parameters: expected Hash (got Array) for param `cell'.
This is the code:这是代码:
= form_for @cell do |f|
#clone
.form.rowField
= f.text_field :name, name: 'cell[][name]', class: "form-control"
= f.select :category_id, options_for_select(Category.all.pluck('name'))
i.fas.fa-plus-circle#addrow
= f.submit "Save", class: "button btn btn-primary"
The javascript code for cloning the form is克隆表单的 javascript 代码是
$('#addrow').click(function () {
var row = document.querySelector('.rowField');
var cloneRow = row.cloneNode(true);
var target = document.querySelector('#clone');
cloneRow.querySelectorAll('input').forEach(function (input) {
input.value = "";
});
target.appendChild(cloneRow);
});
Is there a way to change to name attribute of the select tag from cell[category_id]
to cell[][category_id]
?有没有办法将 select 标签的 name 属性从cell[category_id]
更改为cell[][category_id]
?
我通过将选择字段的名称更改为:
= f.select :category_id, options_for_select(Category.all.pluck('name')), {}, {name: 'cell[][category_id]'}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.