Elixir 未加载ProgrammingPhoenix Rumbl.Web
我试图将登录功能作为本书的一部分来实现,但遇到了以下问题:Elixir 未加载ProgrammingPhoenix Rumbl.Web,elixir,phoenix-framework,Elixir,Phoenix Framework,我试图将登录功能作为本书的一部分来实现,但遇到了以下问题: == Compilation error on file web/controllers/user_controller.ex == ** (CompileError) web/controllers/user_controller.ex:39: undefined function page_path/2 (stdlib) lists.erl:1338: :lists.foreach/2 (stdlib)
== Compilation error on file web/controllers/user_controller.ex ==
** (CompileError) web/controllers/user_controller.ex:39: undefined
function page_path/2
(stdlib) lists.erl:1338: :lists.foreach/2
(stdlib) erl_eval.erl:670: :erl_eval.do_apply/6
(elixir) lib/kernel/parallel_compiler.ex:117: anonymous fn/4 in
Kernel.ParallelCompiler.spawn_compilers/1
我的模块如下所示:
defmodule Rumbl.UserController do
use Rumbl.Web, :controller
plug :authenticate when action in [:index, :show]
alias Rumbl.User
def index(conn, _params) do
users = Repo.all(User)
render conn, "index.html", users: users
end
def show(conn, %{"id" => id}) do
user = Repo.get(Rumbl.User, id)
render conn, "show.html", user: user
end
def new(conn, _params) do
changeset = User.changeset(%User{})
render conn, "new.html", changeset: changeset
end
def create(conn, %{"user" => user_params}) do
changeset = User.registration_changeset(%User{}, user_params)
case Repo.insert(changeset) do
{:ok, user} ->
conn
|> put_flash(:info, "#{user.name} created!")
|> redirect(to: user_path(conn, :index))
{:error, changeset} ->
render(conn, "new.html", changeset: changeset)
end
end
defp authenticate(conn, _opts) do
if conn.assigns.current_user do
conn
else
conn
|> put_flash(:error, "You must be logged in to access that page")
|> redirect(to: page_path(conn, :index))
|> halt()
end
end
end
我知道我的控制器来自这里:
defmodule Rumbl.Web do
@moduledoc """
A module that keeps using definitions for controllers,
views and so on.
This can be used in your application as:
use Rumbl.Web, :controller
use Rumbl.Web, :view
The definitions below will be executed for every view,
controller, etc, so keep them short and clean, focused
on imports, uses and aliases.
Do NOT define functions inside the quoted expressions
below.
"""
def model do
quote do
use Ecto.Schema
import Ecto
import Ecto.Changeset
import Ecto.Query
end
end
def controller do
quote do
use Phoenix.Controller
alias Rumbl.Repo
import Ecto
import Ecto.Query
import Rumbl.Router.Helpers
import Rumbl.Gettext
end
end
def view do
quote do
use Phoenix.View, root: "web/templates"
# Import convenience functions from controllers
import Phoenix.Controller, only: [get_csrf_token: 0, get_flash: 2, view_module: 1]
# Use all HTML functionality (forms, tags, etc)
use Phoenix.HTML
import Rumbl.Router.Helpers
import Rumbl.ErrorHelpers
import Rumbl.Gettext
end
end
def router do
quote do
use Phoenix.Router
end
end
def channel do
quote do
use Phoenix.Channel
alias Rumbl.Repo
import Ecto
import Ecto.Query
import Rumbl.Gettext
end
end
@doc """
When used, dispatch to the appropriate controller/view/etc.
"""
defmacro __using__(which) when is_atom(which) do
apply(__MODULE__, which, [])
end
end
我已经检查了代码很多次,但什么都看不到。任何帮助都将不胜感激。您的路由器有问题。在控制台中尝试混音phoenix.routes。然后找到合适的
something\u路径
函数并使用它。样本输出
page_path GET / Module.PageController :index
user_path GET /login Module.UserController :login
user_path GET /users Module.UserController :index
user_path GET /users/:id Module.UserController :show
user_path POST /users Module.UserController :create
user_path PATCH /users/:id Module.UserController :update
PUT /users/:id Module.UserController :update
user_path DELETE /users/:id Module.UserController :delete
例如,如果我想重定向到主用户路径,我会使用user\u path
,等等
如果可以,请向我们展示您的路由器和mix
phoenix.routes
任务的结果。您的路由器有问题。在控制台中尝试混音phoenix.routes。然后找到合适的something\u路径
函数并使用它。样本输出
page_path GET / Module.PageController :index
user_path GET /login Module.UserController :login
user_path GET /users Module.UserController :index
user_path GET /users/:id Module.UserController :show
user_path POST /users Module.UserController :create
user_path PATCH /users/:id Module.UserController :update
PUT /users/:id Module.UserController :update
user_path DELETE /users/:id Module.UserController :delete
例如,如果我想重定向到主用户路径,我会使用user\u path
,等等
如果可以,请向我们展示您的路由器和mix
phoenix.routes
任务的结果。您可以检查您的router.ex
以找到这一行get/,PageController,:index
1。给我们看看你的路由器。2.请尝试用户路径。您是否可以检查您的路由器.ex
以找到这一行get/”,PageController,:index
1。给我们看看你的路由器。2.请尝试用户路径。您确实是正确的。我缺少page\u路径GET/Module.PageController:index
。是什么线索让你认为这和我的路线有关?奇怪的是,它会因为丢失了一条路径而无法像那样编译?如果这是django,它会正常运行,一旦您发出http请求,它会给您一个“路由不匹配”错误。@clery00这些函数是根据路由生成的,错误是显式的未定义函数页\ u路径…我明白了,谢谢。我花了一些时间来理解这些宏在实践中是如何工作的——这些函数是在编译时根据可用的路由生成的。谢谢,你说得对。我缺少page\u路径GET/Module.PageController:index
。是什么线索让你认为这和我的路线有关?奇怪的是,它会因为丢失了一条路径而无法像那样编译?如果这是django,它会正常运行,一旦您发出http请求,它会给您一个“路由不匹配”错误。@clery00这些函数是根据路由生成的,错误是显式的未定义函数页\ u路径…我明白了,谢谢。我花了一些时间来理解这些宏在实践中是如何工作的——这些函数是在编译时根据可用的路由生成的。谢谢。