Skip to content

Nested form, two levels deep #454

@domi91c

Description

@domi91c

I had my Reform form working with one nested model, but I need that model to contain another nested model. A tutorial has_many steps, a step has_many images. Here's my attempt:

class TutorialForm < Reform::Form

  collection :steps, populator: :step_populator! do
    property :title
    property :body
    validates :title, presence: true
    validates :body, presence: true
    collection :images, populator: :image_populator! do
      property :image
      validates :image, presence: true
    end
  end

  def step_populator!(collection:, index:, **)
    if (item = collection[index])
      item
    else
      collection.insert(index, Step.new)
    end
  end

  def image_populator!(collection:, index:, **)
    if (item = collection[index])
      item
    else
      collection.insert(index, Image.new)
    end
  end
end

Is this totally wrong? It's not working at the moment.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions