Problem in Route

Guys, I’m having a little route problem,
I can’t identify where the problem is.

In my template I added the following code
<%= live_render(@conn, ProjectWeb.LiveHostingwebRequest, session: %{user_id: @conn.assigns.current_user.id}) %>

My Route:

    resources "/requests", RequestController
    live "/check_hostingweb", LiveHostingwebRequest, session: [:user_id]

So far so good … he can carry everything perfectly …
But when I save, it returns me this error

[debug] ** (Phoenix.Router.NoRouteError) no route found for POST /requests/new (ProjectWeb.Router)

(project) lib/phoenix/router.ex:324: ProjectWeb.Router.call/2
(project) lib/project_web/endpoint.ex:1: ProjectWeb.Endpoint.plug_builder_call/2
(project) lib/plug/debugger.ex:122: ProjectWeb.Endpoint."call (overridable 3)"/2
(project) lib/project_web/endpoint.ex:1: ProjectWeb.Endpoint.call/2
(phoenix) lib/phoenix/endpoint/cowboy2_handler.ex:42: Phoenix.Endpoint.Cowboy2Handler.init/4
(cowboy) /home/katmandu/Project/elixir/project/deps/cowboy/src/cowboy_handler.erl:41: :cowboy_handler.execute/2
(cowboy) /home/katmandu/Project/elixir/project/deps/cowboy/src/cowboy_stream_h.erl:296: :cowboy_stream_h.execute/3
(cowboy) /home/katmandu/Project/elixir/project/deps/cowboy/src/cowboy_stream_h.erl:274: :cowboy_stream_h.request_process/3
(stdlib) proc_lib.erl:249: :proc_lib.init_p_do_apply/3

What could it be?

Can you show the form code you’re hitting save in? It’s the form that defines the post path, and your form is trying to POST /requests/new which isn’t a valid route.

Sorry for the delay, yes …

template/request/form.html.eex

<%= form_for @changeset, @action, fn f -> %>
<%= live_render(@conn, ProjectWeb.LiveHostingwebRequest, session: %{user_id: @conn.assigns.current_user.id}) %>
<% end %>

template/live/live_domain_request.ex


defmodule ProjectWeb.LiveDomainRequest do
    use Phoenix.LiveView
    alias Project.Structure
    alias Project.Structure.Request
    alias ProjectWeb.Router.Helpers, as: Routes

  
    def render(assigns) do
      ProjectWeb.RequestView.render("form_domain_live.html", assigns)
    end
 
    def mount(_session, socket) do
      socket =
        socket
        |> assign(:current_step, 1)
        |> assign(:changeset, Structure.change_request(%Request{}))
  
      # {:ok, socket}
      {:ok, assign(socket, paramsdomain: nil, deploy_step: "")}

    end

    def handle_event("validate", %{"request" => params}, socket) do
      changeset = Structure.change_request(params) |> Map.put(:action, :insert)
  
      {:noreply, assign(socket, :changeset, changeset)}
    end

    def handle_event("save", %{"request" => params}, socket) do
      # Pretending to insert stuff if changeset is valid
      # changeset = Structure.change_request(params)
      changeset = Structure.change_request(%Request{})
      require IEx; IEx.pry
      case Structure.create_request(params) do
        
        {:ok, socket} ->
          socket
          |> put_flash(:info, "Pedido criado com sucesso!")
          |> redirect(to: Routes.request_path(socket, :show))
  
        {:error, %Ecto.Changeset{} = changeset} ->
          {:noreply, assign(socket, :changeset, %{changeset | action: :insert})}
      end
    end
end

template/request/form_domain_live.html.Ieex

<form phx-change="check"> 
  <div class="form-group input-group">
    <input type="text" name="paramsdomain" phx-debounce="2000" placeholder="Conferir se o domínio está disponivel." class="form-control"/>
  </div>
  </form>

<%= form_for @changeset, "#", [phx_submit: :save], fn f -> %>
  <%= hidden_input f, :domain, value: @paramsdomain, placeholder: "Consulte se o domínio e valido, logo acima! Ex: domain.com.br", class: "form-control" %>
  <%= error_tag f, :domain %>
<div>
  <%= submit "Cadastrar", phx_disable_with: "Cadastrando...", class: "btn btn-primary" %>
</div>
<% end %>

When I access the direct page by the route, I can enter normally …
http://localhost:4000/check_hostingweb

When accessing the request page(http://localhost:4000/request/new), it returns me this error …

You have a form inside a form, which isn’t valid HTML. Instead of doing form_for and then a live_render inside of that, I would live_render and pass in the changeset, and do the whole form inside the changeset.

2 Likes