gpt4 book ai didi

routes - 带有两个参数的路由的未定义 [controller]_path

转载 作者:行者123 更新时间:2023-12-04 18:30:07 25 4
gpt4 key购买 nike

我在我的 Phoenix 应用程序中创建了一个 Controller ,名为 ProgressController .这是我的路由器文件的样子:

defmodule MyTestApp.Router do
use MyTestApp.Web, :router

pipeline :api do
plug :accepts, ["json"]
end

scope "/", MyTestApp do
pipe_through :api

get "/users/:user_id/courses/:course_id", ProgressController, :show
end
end

当我跑 mix phoenix.routes它输出:
progress_path  GET   /users/:user_id/courses/:course_id  MyTestApp.ProgressController :show

我有以下测试,它使用 progress_path :

test "shows the user's progress in the given course", %{conn: conn} do
# prepare some stuff
conn = get conn, progress_path(conn, :show, %{user_id: 1, course_id: 7})
# assert some stuff
end
progress_pathmix phoenix.routes 同名给了我,我很惊讶在运行我的测试时看到这个错误消息:

编译 11 个文件 (.ex)
** (CompileError) test/controllers/progress_controller_test.exs:12: undefined function progress_path/3
(stdlib) lists.erl:1338: :lists.foreach/2
(stdlib) erl_eval.erl:670: :erl_eval.do_apply/6
(elixir) lib/code.ex:363: Code.require_file/2
(elixir) lib/kernel/parallel_require.ex:57: anonymous fn/2 in Kernel.ParallelRequire.spawn_requires/5

20:34:46.402 [error] GenServer #PID<0.242.0> terminating
** (CompileError) test/controllers/progress_controller_test.exs:12: undefined function progress_path/3
(stdlib) lists.erl:1338: :lists.foreach/2
(elixir) src/elixir_module.erl:113: :elixir_module.do_compile/5
(stdlib) erl_eval.erl:670: :erl_eval.do_apply/6
(elixir) src/elixir.erl:223: :elixir.erl_eval/3
(elixir) src/elixir.erl:211: :elixir.eval_forms/4
(elixir) src/elixir_compiler.erl:66: :elixir_compiler.eval_compilation/3
(elixir) src/elixir_lexical.erl:17: :elixir_lexical.run/3
(elixir) src/elixir_compiler.erl:30: :elixir_compiler.quoted/3
(elixir) lib/code.ex:363: Code.require_file/2
(elixir) lib/kernel/parallel_require.ex:57: anonymous fn/2 in Kernel.ParallelRequire.spawn_requires/5

我将问题缩小到该路由的 URL 中有 2 个参数这一事实,但我不知道如何实际解决此问题。我究竟做错了什么?

最佳答案

经过大量的搜索、尝试和错误,我发现 path函数需要参数列表,而不是 Map .像这样:

conn = get conn, progress_path(conn, :show, 1, 7)

关于routes - 带有两个参数的路由的未定义 [controller]_path,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39812524/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com