如何解决Rails 5:如何处理 simple_form 中的序列化字符串数组?
作为标题,我在模型中有一个序列化的字符串列:
class Product < ApplicationRecord
serialize :ingredient_fields,Array
end
ingrdient_field 有许多字符串,但存储在产品模型中的一列中。它不是嵌套对象。
但我不确定如何使用 simple_form 处理这些动态文本输入,或者我是否可以使用 cocoon 或 nested_form_fields 来处理这些行为。我用这些 gem 尝试了许多不同的方法来生成 html,例如:
<form>
<input name="product[ingredient_fields][]"> <button>remove</button>
<input name="product[ingredient_fields][]"> <button>remove</button>
<button>add ingredient</button>
</form>
但都失败了。
还是我只能自己写html和js?
解决方法
您可以在输入名称中插入索引,例如:
<input name="product[ingredient_fields][1]">
<input name="product[ingredient_fields][2]">
所以 rails 将收到一系列产品[ingredient_fields]
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。