简体   繁体   English

如何创建带有可变输入字段的rails simple_form

[英]How do I create a rails simple_form with variable input fields

I have a PurchaseOrder Model that has_many Item s. 我有一个has_many ItemPurchaseOrder模型。 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. PurchaseOrder表单需要变量输入字段,该字段还将保存Items ,单击“ Add按钮将增加页面中的字段。

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? 如何创建一个simple_form,将这些多个字段的结果作为数组发布到我现有的PurchaseOrder控制器中,在这里我可以处理和添加这些记录?

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 . 您需要使用simple_fields_forsimple_form宝石 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. 如果要通过“ Add new按钮添加新的关联模型,则需要创建一个新行。 There is no code in pure simple_form that will help you with that. simple_form中没有代码可以帮助您。 I found gem cocoon . 我找到了宝石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. 您可以看看它是如何实现的,并制定自己的照明解决方案。

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

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