Using live view with inputs_for

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 basically just using liveview at the moment for nested views with inputs_for (with add and delete entry buttons). What problems are you seeing?

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

  def changeset(struct, params) do
    |> cast(params, [:name])
    |> cast_assoc(:bars, required: true)
    |> validate_required([:name])

  def new() do
    changeset(%__MODULE{bars: [%Bar{}]})
def MyLive do
  def mount(_session, socket) do
    {:ok, assign(socket, %{changeset:})}

  def render(assigns) do
    <%= f = form_for @changset, "#", [phx_submit: :save] %>
      <%= text_input f, :name %>

      <%= inputs_for f, :bars, fn fb ->
        <%= text_input fb, :key %>
      <% end %>
    <% end %>

Is that just a typo in the copy and paste or is it like that in your actual code?

Have you tried adding something like

<%= inspect %>

in there to see what you’re getting as well?

That is just a typo. I think I have it figured out now. I’m not really sure what changed though :frowning:.

1 Like

Weirdness. It looked pretty much the same as mine other than that except I don’t tend to default any nested elements.

The issue has been officially solved: