I am trying to migrate an app that uses controllers to live view. I have a form component that I believe handles the submit event and navigates to a new page. However, when I try to submit the form, I get this error:
Phoenix.Router.NoRouteError at POST /posts/new
no route found for POST /posts/new (MicroblogWeb.Router)
Available routes
GET / MicroblogWeb.PostLive.Index nil
GET /posts/new MicroblogWeb.PostLive.New nil
GET /dev/dashboard/css-:md5 Phoenix.LiveDashboard.Assets :css
GET /dev/dashboard/js-:md5 Phoenix.LiveDashboard.Assets :js
GET /dev/dashboard Phoenix.LiveDashboard.PageLive :home
GET /dev/dashboard/:page Phoenix.LiveDashboard.PageLive :page
GET /dev/dashboard/:node/:page Phoenix.LiveDashboard.PageLive :page
* /dev/mailbox Plug.Swoosh.MailboxPreview []
My form component:
defmodule MicroblogWeb.PostLive.FormComponent do
use MicroblogWeb, :live_component
alias Microblog.Feed
@impl true
def render(assigns) do
~H"""
<div>
<.simple_form
:let={f}
for={@form}
id="post-form"
autocomplete="off"
novalidate
aria-labelledby="post-form-heading"
data-phx-target={@myself}
data-phx-change="validate"
data-phx-submit="save"
class={[
"bg-background text-foreground",
"space-y-fl-xs px-fl-sm-lg py-fl-xs mx-auto max-w-xl rounded-sm"
]}
>
<.page_heading id="post-form-heading">{@title}</.page_heading>
<.error :if={@form.action}>{gettext("Oops something went wrong!")}</.error>
<.textarea_field
field={f[:body]}
variant="outline"
rows="3"
label={gettext("Text")}
label_class="sr-only"
placeholder="Tell 'em how you really feel"
maxlength="280"
content_sizing
/>
<:actions>
<.button variant="default" color="primary" data-phx-disable-with={gettext("Posting…")}>
{gettext("Post")}
</.button>
</:actions>
</.simple_form>
</div>
"""
end
@impl true
def mount(socket) do
{:ok, assign(socket, :submit_attempted, false)}
end
@impl true
def update(%{post: post} = assigns, socket) do
{:ok,
socket
|> assign(assigns)
|> assign_new(:form, fn ->
to_form(Feed.change_post(post))
end)}
end
@impl true
def handle_event("validate", %{"post" => post_params}, socket) do
if socket.assigns.submit_attempted do
changeset = Feed.change_post(socket.assigns.post, post_params)
{:noreply, assign(socket, form: to_form(changeset, action: :validate))}
else
{:noreply, socket}
end
end
def handle_event("save", %{"post" => post_params}, socket) do
save_post(socket, socket.assigns.action, post_params)
end
defp save_post(socket, :edit, post_params) do
case Feed.update_post(socket.assigns.post, post_params) do
{:ok, _post} ->
{:noreply,
socket
|> put_flash(:success, gettext("Post updated successfully"))
|> push_navigate(to: socket.assigns.navigate)}
{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, form: to_form(changeset), submit_attempted: true)}
end
end
defp save_post(socket, :new, post_params) do
case Feed.create_post(post_params) do
{:ok, _post} ->
{:noreply,
socket
|> put_flash(:success, gettext("Post created successfully"))
|> push_navigate(to: socket.assigns.navigate)}
{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, form: to_form(changeset))}
end
end
end
I have read that this happens when the event is not handled, but to the best of my knowledge I am handling the event. What is causing the NoRouteError?