form_for non-AR model - fields_for Array attribute doesn't iterate

后端 未结 3 398
野趣味
野趣味 2020-12-30 09:15

I\'m having trouble getting fields_for to work on an Array attribute of a non-ActiveRecord model.

Distilled down, I have to following:

models/pa

3条回答
  •  一生所求
    2020-12-30 09:42

    I think that it can be done without the need of each:

    = form_for @parent, :url => new_parent_path do |f|
      = f.fields_for :bars do |r|
        = r.object.inspect
    

    You need to set some methods that are expected in the parent class to identify the collection.

    class Parent
      def bars_attributes= attributes
      end
    end
    

    And you also will need to make sure that the objects in the array respond to persisted (so you cannot use strings) :(

提交回复
热议问题