简体   繁体   中英

How do I create a rails simple_form with variable input fields

I have a PurchaseOrder Model that has_many Item s. The form for PurchaseOrder needs variable input fields that will also save Items , where clicking the Add button will increase the fields in the page.

Here's what it will look like:

在此处输入图片说明

In order to achieve this:

How do I create a simple_form that will post the result of these multiple fields as an array to my existing PurchaseOrder controller where I can process and add these records?

Bonus: how would I handle this via Cucumber?

Edit: Why would you downvote a self contained question? Leave comments to explain when you downvote , please.

使用Cocoon作为创建多个字段的嵌套表单。

You need to use simple_fields_for from simple_form gem . This allows you to work with attributes of associated models.

If you want to add new associated models via Add new button you need to create a new row. There is no code in pure simple_form that will help you with that. I found gem cocoon . It looks like it's what you're looking for. You can take a look how it's implemented there and make your own light solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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