redpoll

redpoll

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" %>
  </form>
<% end %>
  def handle_event("add_points", %{"player_id" => %{"player_id" => player_id}, "player_points" => %{"points" => points_for_player}}, socket) do
(...)
    Points.assign_player_points(points_for_player)
    {:noreply, socket}
  end

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

Most Liked

moogle19

moogle19

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

Where Next?

Popular in Questions Top

_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
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
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
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
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Other popular topics Top

aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43806 214
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39523 209
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54250 245
New

We're in Beta

About us Mission Statement