neurodynamic
I have an app with posts and comments, and a “Post a comment” form. The “Post a comment” form is a LiveComponent and most of the code in it has been copied from the default implementation for the way the new/update form works on default-generated index and show pages created via the phx.gen.live generator.
However, one piece of behavior that works fine with generated index/show pages is not working for the comment form and I can’t figure out why. When a user creates a new comment, this code should trigger a flash message:
{:ok, comment} ->
notify_parent({:comment_saved, comment})
{:noreply,
socket
|> put_flash(:info, "Comment created successfully")}
but it doesn’t.
If I instead trigger a flash message in the Post show.ex file that the comment form is rendered from, it works fine. That is, this works:
@impl true
def handle_info({FormComponent, {:comment_saved, comment}}, socket) do
{:noreply,
socket
|> stream_insert(:comments, comment)
|> put_flash(:info, "Comment saved successfully")}
end
But I’d rather the code for that flash message stay in the relevant form_component file.
The LiveComponent is invoked in the show.html.heex file like this:
<.live_component
module={FormComponent}
id={"post-#{@post.id}-new-comment"}
post={@post}
current_user={@current_user}
action={:new}
comment={@new_comment}
/>
If there’s any other code that would be helpful to provide to help track down why this isn’t working in this case, let me know.
Trending in Questions
Other Trending 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
- #blog-post
- #phoenix_html
- #iex
- #graphql
- #ai
- #genstage
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex











Showing Posts 1 to 5- Show Best Posts
- Show All Posts (oldest first)
- Show All Posts (newest first)
mindok
The
socketfor the live component is different to thesocketfor the parent live view.To update the live view socket you will need to send a message to the parent liveview and handle the message there to update the correct socket.
In the live component (in place of
put_flash):In the live view:
neurodynamic
That makes sense, but if that’s true, why do flash messages work fine without that in the standard generated
phx.gen.livecode?The only obvious difference I can see is that the standard generated code is calling
push_patchafter the flash message is set. Does that call make a difference to how this works?ivanhercaz
Interesting and really good post by @sevenseacat about how to update messages from a LiveComponen that maybe useful for you:
mindok
Good question - have you tried commenting out the
push_patchfrom the generated code to see what happens?trisolaran
Yes: Phoenix.LiveView — Phoenix LiveView v1.2.5