theAntimon

theAntimon

Getting "no route found for POST" in a liveView form

Hey people.

I’m trying to make my way through this article

https://littlelines.com/blog/2020/07/06/building-a-video-chat-app-in-phoenix-liveview

I’m not very experienced with Phoenix and LiveView I have to say. Just finding my way into the web topic. Anyway.. Since this article is already some days old I need to change a lot to make it work with today’s phoenix. But I got stuck pretty early. The form to make a new room was based on ~L and I have to move it into the whole ~H world. This is the render/1 function I came up with:

  @impl true
  def render(assigns) do
    ~H"""
    <h1>Create a New Room</h1>

    <.form :let={f} for={@changeset} phx-change="validate" phx-submit="add_match_result">
      <.input field={{f, :title}} name="title" type="text" label="title" value={nil} />
      <.input field={{f, :slug}} name="slug" type="text" label="slug" value={nil} />
      <.button phx-disable-with="Saving...">Save</.button>
    </.form>
    """
  end

I thought it is pretty straight forward and I expected a callback to handle_event/3. But this is not happening. Instead I get an error inside my browser telling me

no route found for POST /room/new (LittlechatWeb.Router)

after I clicked the save button. As far as I understood liveView a POST request is the wrong thing to happen. But what is going wrong here?

This is the maybe important part of my router.ex if that helps:


  scope "/room" do
    live "/new", LittlechatWeb.Room.NewLive, :new
    live "/:slug", LittlechatWeb.Room.ShowLive, :show
  end

Most Liked

kigila

kigila

I found the issue. I have soime javascript in my app.js which were messing up with my DOM. So guys, look at your javascript and do some testing. desable some and see what happen. hope this helps

caleb-bb

caleb-bb

Can we see the event handler for add_match_result? Are you sure it’s not being called?

Generally, if a POST request is being sent, Liveview has failed somehow. Have you checked your js console in-browser as well?

arcanemachine

arcanemachine

I strongly advise you to bail on that tutorial and try to find something a little newer. LiveView changes fast (less so as time goes on) and old content is likely to lead you down blind alleys. Here a couple good resources I found helpful when I was starting:

https://github.com/dwyl/phoenix-liveview-counter-tutorial

Also the GitHub account from the second link has a couple other good repos to learn LiveView concepts. Some of their implementations are a bit off (e.g. their Presence implementation was kinda half-baked IMO) but at least the code will compile.

Now to justify my initial position with some aimless ranting (I’d say I’m in the ‘advanced beginner’ skill level for Phoenix/LiveView). Yes, the code you posted should not result in any POST requests. If you look at the rendered HTML in your DevTools, you will probably find that the form has method="POST" in it (not sure why or how) because I believe that by default, a basic form should render a GET request. So that’s all weird.

If you were to create a POST endpoint in your router, it would look like this:

post "/create", YourController, :create

Of course, you didn’t, and since you used phx-submit in your <.form> component, that should mean that no POST request should be emitted at all. The Phoenix LiveView JS should intercept the form’s submit event and pass the event data to your Elixir code through the websocket instead of doing an HTTP POST request. But it’s not. Maybe try using the <.simple_form> component that’s part of the newer Phoenix function components? That’s the only thing I can think of off the top of my head. Perhaps the older <.form> Phoenix.HTML component may not accept the phx-submit attribute… just a guess. Check the HTML in your browser to see if the phx-submit attribute actually shows up in your <form> element. That would be my suspicion.

But yeah, since the browser is doing a POST request, I’m 99% sure that means that it’s not doing the websocket request, which is why you’re not seeing anything in the Elixir terminal.

TLDR; I think the <form> element in your HTML (in the browser) doesn’t actually have the phx-submit attribute and it may be because <.form> isn’t passing the attribute through. Either way, I strongly recommend finding a newer tutorial to work with.

Last Post!

walker

walker

I also encountered this problem, this problem occurs because of the liveview client (app.js) is not returned to the browser. solved it by adding pipe_through. as follows.

  pipeline :browser do
    plug :accepts, ["html"]
    plug :fetch_session
    plug :fetch_live_flash
    plug :put_root_layout, html: {YourAppWeb.Layouts, :root}
    plug :protect_from_forgery
    plug :put_secure_browser_headers
    plug :fetch_current_user
  end

  scope "/room" do
   pipe_through [:browser]
    live "/new", LittlechatWeb.Room.NewLive, :new
    live "/:slug", LittlechatWeb.Room.ShowLive, :show
  end

Where Next?

Popular in Questions Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44139 214
New

We're in Beta

About us Mission Statement