I have two schemas (User and Post) in separate contexts with auto-generated live views for each and associated with a typical user has_many posts
relation
Creating/editing a new Post
via app/posts
(i.e. post_live/index.html.leex
) works fine (as well as associating with current user).
But, I’d like to let users create posts from their profile (i.e. user_live/show.html.leex
). So, I tried to pass the PostLive.FormComponent
to the live_modal
as usual:
<%= live_patch "+New Post", to: Routes.user_show_path(@socket, :new_post, @user) %>
<%= if @live_action in [:new_post] do %>
<%= live_modal @socket, App.PostLive.FormComponent,
id: @post.id || :new,
title: @page_title,
action: @live_action,
post: @post,
return_to: Routes.user_show_path(@socket, :posts, @user) %>
<% end %>
And added the action handler to the UserLive.Show
module:
defp apply_action(socket, :new_post, _params) do
socket
|> assign(:page_title, "New Post")
|> assign(:post, %Post{})
end
Problems:
- Closing the standard
live_modal
throws error:
** (FunctionClauseError) no function clause matching in App.UserLive.Show.handle_event/3
…UserLive.Show.handle_event(“close”, %{}…
But, why the handle_event("close", _, socket)
in the App.ModalComponent
isn’t triggered?
What am I missing here?
- SOLVED: Saving a post via
UserLive.Show
view doesn’t work (the form just hangs forever).
looks like the “save” event isnothandled by thehandle_event("save", params, socket)
in thePostLive.FormComponent
.
So, how do I create a new Post via the User liveview?
UPDATE:
The second issue is solved by adding correct action to action handler in the PostLive.FormComponent
as:
defp save_post(socket, action, post_params) when action in [:new, :new_post]