【问题标题】:Phoenix LiveView form_for raising NoRouteError at POSTPhoenix LiveView form_for 在 POST 时引发 NoRouteError
【发布时间】:2020-03-26 07:40:49
【问题描述】:

所以我正在尝试制作一个 phoenix 实时取景应用程序,当我尝试将它实施到我已经存在的项目中时遇到了一个问题。我使用 phoenix.html 提供的 form_for 制作了一个表单,它在我的 live socket 上向服务器发出一个 post 请求。我不认为这不应该发生,因为我成功完成了演示,它允许我发布到实时视图。这是错误:

[debug] ** (Phoenix.Router.NoRouteError) no route found for POST /test_live/1 (OkayfivePhxWeb.Router)
(okayfive_phx 0.1.0) lib/phoenix/router.ex:330: OkayfivePhxWeb.Router.call/2
(okayfive_phx 0.1.0) lib/okayfive_phx_web/endpoint.ex:1: OkayfivePhxWeb.Endpoint.plug_builder_call/2
(okayfive_phx 0.1.0) lib/plug/debugger.ex:130: OkayfivePhxWeb.Endpoint."call (overridable 3)"/2
(okayfive_phx 0.1.0) lib/okayfive_phx_web/endpoint.ex:1: OkayfivePhxWeb.Endpoint.call/2
(phoenix 1.4.16) lib/phoenix/endpoint/cowboy2_handler.ex:42: Phoenix.Endpoint.Cowboy2Handler.init/4
(cowboy 2.7.0) /mnt/c/Users/oriont/dev/okayfive_phx/deps/cowboy/src/cowboy_handler.erl:41: :cowboy_handler.execute/2
(cowboy 2.7.0) /mnt/c/Users/oriont/dev/okayfive_phx/deps/cowboy/src/cowboy_stream_h.erl:320: :cowboy_stream_h.execute/3
(cowboy 2.7.0) /mnt/c/Users/oriont/dev/okayfive_phx/deps/cowboy/src/cowboy_stream_h.erl:302: :cowboy_stream_h.request_process/3
(stdlib 3.11.2) proc_lib.erl:249: :proc_lib.init_p_do_apply/3

这是实时控制器:(test_live.ex)

defmodule OkayfivePhxWeb.TestLive do
  use Phoenix.LiveView

  alias OkayfivePhx.Quizzes

  def mount(_params, _session, socket) do
    {:ok, assign(socket, count: 0)}
  end

  def handle_params(%{"id" => id}, _url, socket) do
    {:noreply,
     assign(socket, %{
       user: id,
       changeset: Quizzes.change_student_answer(%Quizzes.StudentAnswer{})
     })}
  end

  def render(assigns), do: OkayfivePhxWeb.QuizView.render("test.html", assigns)

  def handle_event("save", %{"user" => _user_params}, socket) do
    IO.inspect("GET SAVED ON")

    {:noreply, socket}
  end
end

这里是 test.html.leex:

<%= f = form_for @changeset, "#", [phx_submit: :save, phx_hook: "SavedForm"] %>

  <%= label f, :answer %>
  <%= text_input f, :answer, phx_debounce: "blur" %>
  <%= error_tag f, :answer %>

  <div>
    <%= submit "Save", phx_disable_with: "Saving..." %>
  </div>
</form>

所以我尝试做其他事情,这是一个仅使用 html 和属性的表单:

<form phx-change="validate" phx-submit="save">
  <input type="text" name="user[email]" phx-debounce="blur"/>
  <input type="text" name="user[username]" phx-debounce="2000"/>
  <input type="submit" value="Submit">
</form>

令我惊讶的是,此表单不会引发 no route 错误。但是,它仍然没有调用我在 test_live.ex 中定义的 handle_event 函数

这是我的路由器:

pipeline :browser do
    plug :accepts, ["html"]
    plug :fetch_session
    plug :fetch_live_flash
    plug :protect_from_forgery
    plug :put_secure_browser_headers
  end

scope "/", OkayfivePhxWeb do
    pipe_through :browser
...
    live "/test_live/:id", TestLive
...

  end

感谢任何帮助。谢谢!

【问题讨论】:

  • 你能显示 mix.ex 文件吗?
  • 它最终与 mix.ex 文件无关。不过,感谢您的关注!

标签: elixir phoenix-framework phoenix phoenix-live-view


【解决方案1】:

我真的想通了,我现在觉得很愚蠢。原因是我没有使用带有 liveview 渲染的布局,意思是这个东西:

<%= @inner_content %>

确保这在您的布局中,并且布局确实有效。

如果没有,您的表单将发出一个发布请求,而不是通过网络套接字。

【讨论】:

  • 你不得不这么说真是太糟糕了。我问这个问题的原因是肯定的:我确实缺乏对实时取景的基本了解,那是因为这是我第一次使用它。请,如果您的 cmets 没有帮助和/或居高临下,您可以避免发布它们。我忘记添加的另一件事是我使用的是&lt;%= render @view_module, @view_template, assigns %&gt; 模板,因此请确保将布局中的这一行替换为@inner_content 行。
  • 我遇到了这个确切的问题,但这个解决方案对我不起作用。我已经验证我是从新鲜混合的 phx.gen.live 中使用 elixir 1.10.4 和 erlang 22.3.1 获得的。我的布局确实有效,并且使用了@inner_content。 :sad_face:
  • 我想通了。我需要关注这部分文档hexdocs.pm/phoenix_live_view/…
  • 也不要忘记 head 部分中的 - 一天中几乎要花 30 分钟来解决这个问题 - 感谢您的解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-07-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多