Embedded map array forms

I am trying to render a form with an embedded schema like in the example in the docs:
https://hexdocs.pm/phoenix_html/Phoenix.HTML.Form.html#module-nested-inputs

defmodule User do
  use Ecto.Schema

  schema "users" do
    field :name
    embeds_one :permalink, Permalink
  end
end

defmodule Permalink do
  use Ecto.Schema

  embedded_schema do
    field :url
  end
end

but the relationship with my embedded schema is embeds_many, not embeds_one.
The docs don’t have such an example.
Does anyone know how to render a form in such a case?

Thank you!

inputs_for supports embeds_many. The difficulty here is building a UI that’s going to work well for users. The challange here is that you want to manage a collection in a single form - this would normally be a good job for a SPA application.

Yeah, SPA makes sense in this context, but I was wodering if there is a way to do it using LiveView.

ˋinputs_forˋ works for embeds_one just like for embeds_many. It just happens to render one set of fields instead of multiple.

1 Like

thanks! I think I figured it out - the inputs were not rendered at all, even empty, until I put a default list of structs with nullified fields into the changeset. This part was not clear to me.
I am going to try to implement “add/remove set” buttons with LiveView.

Any success ? If you could nudge me in the right direction, that’ll be quite helpful

The solution I ended up adopting revolves around setting the right “name” attribute to the input tag.
For example: user[param_1][index][nested_param_1].

Try playing around with “name” and see how it affects the parameters passed from the form.

1 Like