ruby-on-railscucumbersimple-form

How do I create a rails simple_form with variable input fields


I have a PurchaseOrder Model that has_many Items. 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 PurchaseOrdercontroller where I can process and add these records?

Bonus: how would I handle this via Cucumber?


Solution

  • 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.