I am testing out live view. I have a situation where I wanted to use it with a nested form. Usually this would be done with inputs_for/{3,4}. However, the module documentation for Phoenix.HTML.Form has the following in it.
Because Phoenix.LiveView is unable to compute diffs inside anonymous functions, Phoenix.HTML provides form_for/3 that works without passing an anonymous function.
This leads me to believe that using inputs_for would also not be able to work properly (and I have not been able to make it work yet). I was wondering if there is a known way to make it work or a workaround?
I’m just not seeing the fields for my has_many relation. I have something like the following code.
defmodule Foo do
schema "foos" do
field :name, :string
has_many :bars, Bar
end
def changeset(struct, params) do
struct
|> cast(params, [:name])
|> cast_assoc(:bars, required: true)
|> validate_required([:name])
end
def new() do
changeset(%__MODULE{bars: [%Bar{}]})
end
end
def MyLive do
def mount(_session, socket) do
{:ok, assign(socket, %{changeset: Foo.new()})}
end
def render(assigns) do
~L"""
<%= f = form_for @changset, "#", [phx_submit: :save] %>
<%= text_input f, :name %>
<%= inputs_for f, :bars, fn fb ->
<%= text_input fb, :key %>
<% end %>
<% end %>
"""
end
end