Need help with handle_events/3, getting error: (FunctionClauseError) no function clause matching in MyWeb.PostLive.Index.handle_event/3

Hi,

So I’m trying to get my head around liveview and feel like I understand some of the basics but I’m getting stumped on how handle_events/3 works. I can create posts, and create comments with associations to the posts. I can display the posts and comments on the same page as well as on a different pages.

I can display the ‘post’ at:

/user/:username

And if I click on the ‘post’ I can redirect to:

/user/:username/posts/14

Edit: posts/14 is the post ID, not the comment ID that I am trying to delete

For both these locations I can use the below to delete the post:

<%= link “Delete”, to: “#”, phx_click: “delete post”, phx_value_id: @post.id, data: [confirm: “Are you sure?”] %>

def handle_event(“delete post”, %{“id” => id}, socket) do
post = Posts.get_post!(id)
{:ok, _} = Posts.delete_post(post)
{:noreply, assign(socket, :posts, Posts.list_posts())}
end

However, I cannot delete comments using the same method.

<%= link “Delete”, to: “#”, phx_click: “delete comment”, phx_value_id: @comment.id, data: [confirm: “Are you sure?”] %>

def handle_event(“delete comment”, %{“comment_id” => comment_id}, socket) do
comment = Comments.get_comment!(comment_id)
{:ok, _} = Comments.delete_comment(comment)
{:noreply, assign(socket, :comments, Comments.list_comments())}
end

The error I get in the terminal is:

[error] GenServer #PID<0.4639.0> terminating
** (FunctionClauseError) no function clause matching in MyWeb.PostLive.Index.handle_event/3
(jinx 0.1.0) lib/My_web/live/post_live/index.ex:72: MyWeb.PostLive.Index.handle_event(“delete comment”, %{“id” => “11”}

I’ve been playing around with it all day and struggling to make any progress, so any insights are appreciated.

1 Like

This will set an id

not a comment_id…

Here is the confirmation

So there is no pattern match.

What You could do…

def handle_event(“delete comment”, %{“id” => comment_id}, socket) do

or

phx_value_comment_id: @comment.id

But if You have this kind of problem, there is an easy solution, just add a catch all clause.

def handle_event(type, params, socket) do
  IO.inspect type
  IO.inspect params
  {:noreply, socket}
end
1 Like

I’m really sorry I forgot to add that this is the correct ID for the comment I want to delete.

(“delete comment”, %{“id” => “11”}

This is post id 14

/user/:username/posts/14

If I show the post on “/user/:username” the same delete function works. I should have clarified the “14” was the post ID rather than the comment ID as I didn’t show my router. So again, sorry I should have clarified that better.

If I do:

phx_value_comment_id

and try to delete another comment which has an ID of 16 my error becomes:

Index.handle_event(“delete comment”, %{“comment-id” => “16”}

On the plus side I’m new enough that I didn’t know that’s how the phx_value worked and that it could be adjusted like that, so thanks for that bit of info, but unfortunately nothing worked as I think I mislead you by not being clear enough.

Thank you for trying though.