redpoll
Properly jsonb field updating via live form
Hello Devs ![]()
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
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).
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #hex









