简体   繁体   English

如何以 rails 形式添加标签输入功能或如何将数组添加到输入字段?

[英]How to add a tags input functionality in rails form or how to add array to input field?

I am building a form where I want to add the user to add tags in the form.我正在构建一个表单,我想在其中添加用户以在表单中添加标签。

I have completed the functionality but from the front-end only and things got complex to handle.我已经完成了功能,但仅从前端开始,事情变得复杂起来。

I am using Ruby on Rails in backend and html css js jQuery in frontend.我在后端的 Rails 上使用 Ruby,在前端使用 html css js ZF590B4FDA2C30BE28DD3C8C3BZAF5

So far the user can add the tags in the way I want but I didn't find any way of sending the tags as an array in the form.到目前为止,用户可以按照我想要的方式添加标签,但我没有找到任何将标签作为数组发送到表单中的方法。

Is there any way of sending array with forms in input or in rails forms or something else?有没有办法在输入或轨道 forms 或其他东西中发送带有 forms 的阵列?

As I said, I don't know how to send array with form so I am converting that array to string to store it in input field and then back to array using split with comma to print it on form.正如我所说,我不知道如何使用表单发送数组,所以我将该数组转换为字符串以将其存储在输入字段中,然后使用带逗号的拆分返回数组以将其打印在表单上。

But user can add , in input and I don't want to split the string with that comma so that's not the good way of using string!但是用户可以在输入中添加,并且我不想用逗号分割字符串,所以这不是使用字符串的好方法!

If you have any tutorial or link where I can find out how to send that in backend then I will be thankful to you...如果您有任何教程或链接,我可以在其中找到如何在后端发送它,那么我将感谢您...

Thanks谢谢

HTML [] convention. HTML []约定。

Array:大批:

<input type="textbox" name="course[track_codes][]", value="a">
<input type="textbox" name="course[track_codes][]", value="b">
<input type="textbox" name="course[track_codes][]", value="c">

Params received:收到的参数:

{ course: { track_codes: ['a', 'b', 'c'] } }

Another option you have is using:您拥有的另一个选择是使用:

f.input :name, input_html: { multiple: true }

or if you have the simple_form_for gem:或者如果你有 simple_form_for gem:

f.input_field :name, multiple: true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM