Greetings Everyone!!!
Once again I’m lost.
When rendering an index page that shows a referenced field for the first time, everything works fine. The list_trucks()
function is preloading the association to the fuel’s type.
# index.ex
@impl true
def mount(_params, _session, socket) do
{:ok, stream(socket, :trucks, Catalogs.list_trucks())}
end
@impl true
def handle_params(params, _url, socket) do
{:noreply, apply_action(socket, socket.assigns.live_action, params)}
end
# index.html.heex
12: <.table
13: id="trucks"
14: rows={@streams.trucks}
15: row_click={fn {_id, truck} -> JS.navigate(~p"/catalogs/trucks/#{truck}") end}
16: >
17: <:col :let={{_id, truck}} label="Name"><%= truck.trk_descrip %></:col>
18: <:col :let={{_id, truck}} label="Plate"><%= truck.trk_plate %></:col>
19: <:col :let={{_id, truck}} label="Fuel Type"><%= truck.fuels.ful_type %></:col>
# ------------------------------------------------^^^^^^^^^^^^^^^^^^^^
When I click on any truck, it renders the show
page for that truck just fine, and if I edit that truck from there and I change the fuel’s type (with a reference to another table), it returns just fine to the show
page of that truck after saving that modification.
The problem is when I click on the Edit button from the index page, then I change the fuel type and save the modifications. When is rendering the index page again, it shows an error on line 19 of the index.html.heex file, where it already displayed the fuel_type for the reference the first time it displayed the index page.
Since the mount
just happens once, but the handle_params
several times, I thought the handle_params
function would be the right place to reload the trucks list. First I used the assign
function and then I realized the mount
function is using the stream
function.
@impl true
def handle_params(params, _url, socket) do
socket = stream(socket, trucks: Catalogs.list_trucks())
{:noreply, apply_action(socket, socket.assigns.live_action, params)}
end
But even then, I am getting the same error.
(The query that runs from form_component:ex:8 is the one that gathers the options for the select object)
@impl true
def render(assigns) do
assigns =
assigns
|> assign(fuels_options: Catalogs.get_fuels_options())
def get_fuels_options() do
Repo.all(
from f in Fuel,
select: {f.ful_type, f.id},
order_by: f.id
)
end
[debug] HANDLE EVENT "save" in MenuWeb.TruckLive.Index
Component: MenuWeb.TruckLive.FormComponent
Parameters: %{"truck" => %{"trk_active" => "true", "trk_descrip" => "SHOPPING", "trk_fuel" => "2", "trk_plate" => ""}}
[debug] QUERY OK source="trucks" db=2.2ms queue=0.2ms idle=1886.0ms
UPDATE "general"."trucks" SET "trk_fuel" = $1, "updated_at" = $2 WHERE "id" = $3 [2, ~U[2024-12-16 18:40:44Z], 1]
↳ MenuWeb.TruckLive.FormComponent.save_truck/3, at: lib/menu_web/live/truck_live/form_component.ex:60
[debug] Replied in 11ms
[debug] QUERY OK source="fuels" db=0.6ms idle=1889.8ms
SELECT f0."ful_type", f0."id" FROM "general"."fuels" AS f0 ORDER BY f0."id" []
↳ MenuWeb.TruckLive.FormComponent.render/1, at: lib/menu_web/live/truck_live/form_component.ex:8
[debug] HANDLE PARAMS in MenuWeb.TruckLive.Index
Parameters: %{}
::: Listing all the trucks :::
[debug] QUERY OK source="trucks" db=0.7ms idle=1890.7ms
SELECT t0."id", t0."trk_descrip", t0."trk_plate", t0."trk_active", t0."trk_fuel", t0."inserted_at", t0."updated_at" FROM "general"."trucks" AS t0 ORDER BY t0."trk_descrip" []
↳ MenuWeb.TruckLive.Index.handle_params/3, at: lib/menu_web/live/truck_live/index.ex:14
[debug] QUERY OK source="fuels" db=0.3ms idle=1891.7ms
SELECT f0."id", f0."ful_type", f0."inserted_at", f0."updated_at", f0."id" FROM "general"."fuels" AS f0 WHERE (f0."id" = ANY($1)) [[1, 2]]
↳ MenuWeb.TruckLive.Index.handle_params/3, at: lib/menu_web/live/truck_live/index.ex:14
[debug] Replied in 1ms
[error] GenServer #PID<0.739.0> terminating
** (KeyError) key :ful_type not found in: #Ecto.Association.NotLoaded<association :fuels is not loaded>
(menu 0.1.0) lib/menu_web/live/truck_live/index.html.heex:19: anonymous fn/3 in
Up until now, I have three forms where I am displaying a referenced field in the index page, and all three of them have the exact same behaviour: They show just fine the first time, and then, when I edit a record from the index page and change a referenced field, the association is not loaded when rendering the index page again. If I remove that “line 19”, where I try to show referenced field, in all my index.html.heex files everything works fine.
What can I do to have that association loaded again when rendering the page after that edit?
Thanks in advance,
Greg.