【问题标题】:Rails 5: How to handle serialized string array in simple_form?Rails 5:如何处理 simple_form 中的序列化字符串数组?
【发布时间】:2021-02-09 09:06:17
【问题描述】:

作为标题,我在模型中有一个序列化的字符串列:

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?

【问题讨论】:

    标签: ruby-on-rails simple-form cocoon-gem


    【解决方案1】:

    您可以在输入名称中插入索引,例如:

    <input name="product[ingredient_fields][1]">
    <input name="product[ingredient_fields][2]">
    

    所以 rails 会收到一系列产品[ingredient_fields]

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多