Properly jsonb field updating via live form

Hello Devs :slight_smile:
A little background to my question:

I have a live form where I want to pass a jsonb field and cast to a schema. When doing it with simple bigint type for example it can be easily done:

<%= for player <- @players do %>
  <form phx-submit="add_points">
    <%= number_input :player_points, :points, placeholder: "Something" %>
<% end %>
  def handle_event("add_points", %{"player_id" => %{"player_id" => player_id}, "player_points" => %{"points" => points_for_player}}, socket) do
    {:noreply, socket}

Here I have a number input which gets a variable named player_points and I can Repo update a schema with it in a context:

    |> Player.changeset(%{points: points_for_player})
    |> Repo.update()

However, doing it with a jsonb doesn’t seem that easy. I managed to create this:

<%= number_input :points1_for_first_player, :points1_for_first_player, name: "score[game1][points1_for_first_player]", class: "form-control" %>
<%= number_input :points1_for_second_player, :points1_for_second_player, name: "score[game1][points1_for_second_player]", class: "form-control" %>

which means this:

  "score" => %{
    "game1" => %{
      "points1_for_first_player" => "11",
      "points1_for_second_player" => "5"

So far so good, but I receive in the live view points1_for_first_player & points1_for_second_player variables.

And my question is: how should that kind of variable be Repo updated to the jsonb field :score?

field :score, :map


If something is unclear, please, comment.

Best Regards

1 Like

you could use an embedded schema when your inner fields don’t change (take a look at Ecto.Schema — Ecto v3.7.1).
And then you could use inputs_for/3 to generate your input fields (Phoenix.HTML.Form — Phoenix.HTML v3.1.0).

1 Like