我试图将登录功能作为本书的一部分来实现并遇到问题:
== 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
我已多次检查过该代码,无法查看任何内容。任何帮助将不胜感激。
答案 0 :(得分:2)
你的路由器搞砸了。在控制台中尝试mix phoenix.routes
。然后找到适当的something_path
函数并使用它。样本输出。
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_path
等。
如果可以,请向我们展示您的路由器和mix phoenix.routes
任务的结果。