hello
I begin to learn elixir , i try to modify a comment of my app. the post2_id no load when i want to save my comment modified. " key :post2 not found in: %{ socket"
index.html.heex :
<.modal
:if={@live_action in [:new, :edit]}
id="comment2-modal"
show
on_cancel={JS.navigate(~p"/comment2s")}
>
<.live_component
module={BlogWeb.Comment2Live.FormComponent}
id={@comment2.id || :new}
title={@page_title}
action={@live_action}
comment2={@comment2}
post2_id={@post2.id}
current_user_id={@current_user_id}
patch={~p"/comment2s"}
/>
</.modal>
index.ex:
defmodule BlogWeb.Comment2Live.Index do
use BlogWeb, :live_view
alias Blog.Comment2s
alias Blog.Comment2s.Comment2
alias Blog.Post2s.Post2
require Logger
@impl true
def mount(_params, _session, socket) do
{:ok, stream(socket, :comment2s, Comment2s.list_comment2s(socket.assigns[:current_user].id))}
end
thank in advance
Hi @pierre22
!
You are probably missing some assign(socket, :post2, ...)
(or assign_new
or stream_*
…) somewhere, in one of the LiveView callbacks that runs before render which requires that assign to be present.
We would need more code to understand better where 
Hi @RudManusachi ,
form_component.ex
> defmodule BlogWeb.Comment2Live.FormComponent do
> use BlogWeb, :live_component
>
> alias Blog.Comment2s
>
> @impl true
> def render(assigns) do
> ~H"""
> <div>
> <.header>
> <%= @title %>
> <:subtitle>Use this form to manage comment2 records in your database.</:subtitle>
> </.header>
>
> <.simple_form
> for={@form}
> id="comment2-form"
> phx-target={@myself}
> phx-change="validate"
> phx-submit="save"
> >
> <.input field={@form[:post2_id]} type="hidden" value={@post2_id} />
> <.input field={@form[:user_id]} type="hidden" value={@current_user_id} />
> <.input field={@form[:message]} type="text" label="Message" />
> <:actions>
> <.button phx-disable-with="Saving...">Save Comment2</.button>
> </:actions>
> </.simple_form>
> </div>
> """
> end
>
> @impl true
> def update(%{comment2: comment2} = assigns, socket) do
> changeset = Comment2s.change_comment2(comment2)
>
> {:ok,
> socket
> |> assign(assigns)
> |> assign_form(changeset)}
> end
i need to add assign :post2 i think but how add a second assign? thank in advance
@impl true
def mount(_params, _session, socket) do
{:ok, stream(socket, :comment2s, Comment2s.list_comment2s(socket.assigns[:current_user].id))}
end
You have to get the id for post2 from somewhere (this will depend on how your database schemas are modelled). For example, if a post2 has many comment2s then it could be as easy as assigning the post2_id in your index.ex apply_action/3
function.
@impl true
def handle_params(params, _url, socket) do
{:noreply, apply_action(socket, socket.assigns.live_action, params)}
end
defp apply_action(socket, :edit, %{"id" => id}) do
comment2 = Comment2s.get_comment2!(id)
post2_id = comment2.post2_id
socket
|> assign(:page_title, "Edit Comment2")
|> assign(:comment2, comment2)
|> assign(:post2_id, post2_id)
end
1 Like
hello @toberoni
thank for the help,
i succeeded with your help.
the solution:
defp apply_action(socket, :edit, %{“id” => id}) do
comment2 = Comment2s.get_comment2!(id)
post2_id = comment2.post2_id
current_user_id = comment2.user_id
socket
|> assign(:page_title, "Edit Comment2")
|> assign(:comment2, comment2)
|> assign(:post2_id, post2_id)
|> assign(:current_user_id, current_user_id)
end
and i modified :
<.modal
:if={@live_action in [:new, :edit]}
id=“comment2-modal”
show
on_cancel={JS.navigate(~p"/comment2s")}
<.live_component
module={BlogWeb.Comment2Live.FormComponent}
id={@comment2.id || :new}
title={@page_title}
action={@live_action}
comment2={@comment2}
post2_id={@post2_id}
current_user_id={@current_user_id}
patch={~p"/comment2s"}
/>
</.modal>
1 Like