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.