【问题标题】:Indexing on nested form with multiple `fields_for`使用多个“fields_for”对嵌套表单进行索引
【发布时间】:2017-02-17 09:03:11
【问题描述】:

鉴于模型 Album has_many Song 和后者具有本地化字段,例如:

Song#name_en
Song#description_en
Song#name_fr
Song#description_fr
[...]

由于前端的设计,我不能在一个地方为所有歌曲属性做一个f.simple_fields_for :songs,而是需要拆分它:

= f.simple_fields_for :songs do
  = render partial: 'song_en_fields', locals: { f: f, locale: :en }
[...]
= f.simple_fields_for :songs do
  = render partial: 'song_fields', locals: { f: f, locale: :fr }
[...]

结果字段按照应有的方式使用 [0][1] 等进行索引,但是,索引不会在每个 simple_fields_for 上以 0 重新开始,而是不断增加。

我检查了源代码并在 Rails 的 fields_for 中找到了一个 index 选项,但这只是添加了一个额外的索引数组。

simple_fields_for(或fields_for)为同一个集合多次调用时,有没有办法“重置”索引的自动增量?

【问题讨论】:

    标签: ruby-on-rails ruby simple-form fields-for


    【解决方案1】:

    您可以通过将fields_for 放入循环并将child_index: your_index 传递给它来自己设置索引,而不是尝试重置自动增量。

    【讨论】:

    • 是的,我只是来这里发布相同的解决方案。 child_index 似乎完全没有记录,至少在任何地方都没有提到它 here
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-15
    • 1970-01-01
    • 2018-04-23
    • 1970-01-01
    相关资源
    最近更新 更多