itisfilipe

itisfilipe

Not able to generate deeply nested embed forms

I am trying to use two nested inputs_for with no success. Essentially, I have this (abbreviated for simplicity):

# CalendarApp.Calendars.Calendar
  schema "calendars" do
    ...
    # In the migration I have "add :working_hours, :map"
    embeds_many :working_hours, WorkingHour, on_replace: :delete
  end

# CalendarApp.Calendars.WorkingHour
  embedded_schema do
    ...
    embeds_many :time_intervals, TimeInterval, on_replace: :delete
  end

# CalendarApp.Calendars.TimeInterval
  embedded_schema do
    ...
    # no more embeds
  end

My object

%CalendarApp.Calendars.Calendar{
  __meta__: #Ecto.Schema.Metadata<:loaded, "calendars">,
  id: 1,
  working_hours: [
    %CalendarApp.Calendars.WorkingHour{
      id: "c8140693-be33-4f50-a087-653ce92fa3be",
      weekday: :Sunday,
      enabled: false,
      time_intervals: [
        %CalendarApp.Calendars.TimeInterval{
          id: "0d31837a-d295-4d7b-984f-edfe18858f70",
          start_time: ~T[08:00:00],
          end_time: ~T[12:00:00]
        },
        %CalendarApp.Calendars.TimeInterval{
          id: "e11b03a4-75be-4067-b477-733ebea6b31c",
          start_time: ~T[13:00:00],
          end_time: ~T[18:00:00]
        }
      ]
    },
    %CalendarApp.Calendars.WorkingHour{
      id: "a5a70b7e-577a-42b0-a2f7-4d396c3cd3ad",
      weekday: :Monday,
      enabled: true,
      time_intervals: [
        %CalendarApp.Calendars.TimeInterval{
          id: "865aeea2-d234-47c1-aa1d-b8655c2909f7",
          start_time: ~T[08:00:00],
          end_time: ~T[12:00:00]
        },
        %CalendarApp.Calendars.TimeInterval{
          id: "f80e3863-b801-4964-8c71-f7fca7d567ba",
          start_time: ~T[13:00:00],
          end_time: ~T[18:00:00]
        }
      ]
    },
    ...
  ]
}

how I am trying to present this data

<%= for whf <- inputs_for(f, :working_hours) do %>
  <li class="pb-4 flex items-center justify-between">
    <%= hidden_inputs_for(whf) %>
    <%= label whf, :weekday, class: "text-base font-medium text-gray-900" do %>
      <%= whf.data.weekday %>
    <% end %>
    <%= for intf <- inputs_for(whf, :time_intervals) do %>
      <%= hidden_inputs_for(intf) %>
      <% time_input intf, :start_time %>
      <% time_input intf, :end_time %>
    <% end %>
    <%= error_tag whf, :weekday %>
  </li>
<% end %>

Just the labels and the hidden fields are being rendered

<ul role="list">
  <li class="pb-4 flex items-center justify-between">
    <input id="calendar_working_hours_0_id" name="calendar[working_hours][0][id]" type="hidden" value="c8140693-be33-4f50-a087-653ce92fa3be">
    <label class="text-base font-medium text-gray-900" for="calendar_working_hours_0_weekday">
    Sunday
    </label>
    <input id="calendar_working_hours_0_time_intervals_0_id" name="calendar[working_hours][0][time_intervals][0][id]" type="hidden" value="0d31837a-d295-4d7b-984f-edfe18858f70">
    <input id="calendar_working_hours_0_time_intervals_1_id" name="calendar[working_hours][0][time_intervals][1][id]" type="hidden" value="e11b03a4-75be-4067-b477-733ebea6b31c">
  </li>
  <li class="pb-4 flex items-center justify-between">
    <input id="calendar_working_hours_1_id" name="calendar[working_hours][1][id]" type="hidden" value="a5a70b7e-577a-42b0-a2f7-4d396c3cd3ad">
    <label class="text-base font-medium text-gray-900" for="calendar_working_hours_1_weekday">
    Monday
    </label>
    <input id="calendar_working_hours_1_time_intervals_0_id" name="calendar[working_hours][1][time_intervals][0][id]" type="hidden" value="865aeea2-d234-47c1-aa1d-b8655c2909f7">
    <input id="calendar_working_hours_1_time_intervals_1_id" name="calendar[working_hours][1][time_intervals][1][id]" type="hidden" value="f80e3863-b801-4964-8c71-f7fca7d567ba">
  </li>
  ...
</ul>

I’ve tried many different fields and structures for this form but without success. Does anyone know if this is even possible?

Marked As Solved

woylie

woylie

This should be

<%= time_input intf, :start_time %>
<%= time_input intf, :end_time %>

Where Next?

Popular in Questions Top

earth10
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone. What strikes me is th...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "eq...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
marick
I had some trouble figuring out how to make many-to-many associations work. Once I got it working, I wrote a blog post. Because I'm a nov...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement