本文介绍了具有两个参数的路由的未定义[controller] _path的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I created a controller in my Phoenix application called ProgressController. This is what my router file look like:

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给我的名字,我很惊讶在运行测试时看到此错误消息:

progress_path is the same name that mix phoenix.routes gave me, and I am surprise do see this error message when running my tests:

编译11个文件(.ex)

Compiling 11 files (.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个参数的事实,但我不知道如何实际解决此问题.我在做什么错了?

I narrowed down the problem to the fact that there are 2 params in that route's URL, but I can't figure out how to actually fix this. What am I doing wrong?

推荐答案

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

After a lot of search, trial and errors, I discovered that the path function expects a list of params, not a Map. Like this:

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

这篇关于具有两个参数的路由的未定义[controller] _path的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 12:34