[英]Phoenix LiveView form_for raising NoRouteError at POST
所以我正在嘗試制作一個鳳凰實時查看應用程序,當我嘗試將它實施到我已經存在的項目中時遇到了一個問題。 我使用 phoenix.html 提供的 form_for 創建了一個表單,它在我的實時套接字上向服務器發出了一個 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
任何幫助表示贊賞。 謝謝!
我真的想通了,現在我覺得很愚蠢。 原因是我沒有使用帶有實時視圖渲染的布局,這意味着:
<%= @inner_content %>
確保這在您的布局中,並且布局確實有效。
如果沒有,您的表單將發出發布請求,而不是通過網絡套接字。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.