evao
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?
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
Other Trending Topics
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #hex
- #security










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
mindok
Hi @evao, and welcome to the community.
I’m assuming this is a LiveView. Where you have
data-phx-target,data-phx-changeanddata-phx-submit, you should remove thedata-bit - your events aren’t actually being called and it’s reverting to a standard HTML form action.evao
I changed the binding prefix
The data-phx prefix works in other places
mindok
Have you put some
IO.inspectcalls into your event handlers to confirm they are getting called?evao
I just tried it and it’s not reaching the events. Am I declaring them wrong?
data-phx-hook works fine, so I would assume that data-phx-submit would also be ok
mindok
They look ok. Is there any reason you are changing the binding prefix? It might be worth putting it back to the defaults temporarily just to see whether there’s a bug in its implementation.
evao
I wanted to use valid HTML attributes. I have a different error when I remove the prefix:
So it looks like there’s a bug in the bindingPrefix implementation that doesn’t handle phx-submit.
I think the reason my events aren’t matching is that the textarea_field isn’t setting the correct name anymore.
mindok
Hi @evao, that error is easy to fix… take a look at your
handle_event("validate"...)signature - the params structures don’t match - you’re receiving%{"_target" => ["body"], "_unused_body" => "", "body" => ""}but expecting%{"post" => post_params}. Changing%{"post" => post_params}to%{"body" => post_params}should give you what you want.wrt bindingPrefix - yes, probably a good idea to log an issue on Github - Issues · phoenixframework/phoenix_live_view · GitHub, but I’m not sure it’s needed - there are many high profile sites in production using the default.
steffend
I just tried to reproduce this, but the bindingPrefix option works correct for phx-submit in my simple example:
If you can find out how to reproduce, please open up an issue. We don’t have thorough tests for the bindingPrefix option, so it’s very possible that there are places where it doesn’t work.
I noticed that you set
bindingPrefix: "data-phx", but it should bedata-phx-with an extra dash at the end. Otherwise you’d need to usedata-phxsubmitetc.Phxie
Is this not just an issue based on the actions?
Your simple form doesn’t specify an action to take, but your save_post functions require actions.
evao
When I was using the controller, the textarea name was set to “post[body]”, which pattern matched correctly. I used the generated live view, which seems to expect the same name, but does not set it correctly for some reason.
Thanks for catching the bindingPrefix issue. data-phx-submit is being triggered now.
I have removed the action, but I can’t test that it works until I get the textarea name right.