本文介绍了编程Phoenix:未定义函数page_path/2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的网络应用出现问题,编译错误如下:
I'm having an issue with my web app with a compile error below:
== Compilation error on file web/controllers/auth.ex ==
** (CompileError) web/controllers/auth.ex:49: 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
我知道大多数问题与router.ex
上的缺少路线有关,但看起来这里没有任何问题:
I know most issues have been to do with missing routes on the router.ex
, but it doesn't look like there is any issues here:
scope "/", Rumbl do
pipe_through :browser # Use the default browser stack
get "/", PageController, :index
get "/users/:id", UserController, :show
resources "/users", UserController, only: [:index, :show, :new, :create]
resources "/sessions", SessionController, only: [:new, :create, :delete]
resources "/videos", VideoController
end
scope "/manage", Rumbl do
pipe_through [:browser, :authenticate_user]
resources "/videos", VideoController
end
我的web.ex看起来像这样:
My web.ex looks like this:
def router do
quote do
use Phoenix.Router
import Rumbl.Auth, only: [authenticate_user: 2] # New import
end
end
def controller do
quote do
use Phoenix.Controller
alias Rumbl.Repo
import Ecto
import Ecto.Query, only: [from: 1, from: 2]
import Rumbl.Router.Helpers
import Rumbl.Gettext
import Rumbl.Auth, only: [authenticate_user: 2] # New import
end
end
auth.ex看起来像这样
auth.ex looks like this
defmodule Rumbl.Auth do
import Plug.Conn
import Comeonin.Bcrypt, only: [checkpw: 2, dummy_checkpw: 0]
import Phoenix.Controller
alias Rumbl.Router.Helpers
def init(opts) do
Keyword.fetch!(opts, :repo)
end
def call(conn, repo) do
user_id = get_session(conn, :user_id)
user = user_id && repo.get(Rumbl.User, user_id)
assign(conn, :current_user, user)
end
def login_by_username_and_pass(conn, username, given_pass, opts) do
repo = Keyword.fetch!(opts, :repo)
user = repo.get_by(Rumbl.User, username: username)
cond do
user && checkpw(given_pass, user.password_hash) ->
{:ok, login(conn, user)}
user ->
{:error, :unauthorized, conn}
true ->
dummy_checkpw()
{:error, :not_found, conn}
end
end
def login(conn, user) do
conn
|> assign(:current_user, user)
|> put_session(:user_id, user.id)
|> configure_session(renew: true)
end
def logout(conn) do
configure_session(conn, drop: true)
end
def authenticate_user(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
推荐答案
您可以发布auth.ex文件吗?似乎在第49行有问题.
Can you post auth.ex file? It seems there is an issue in it on line 49.
请更改此行:
|> redirect(to: page_path(conn, :index))
到
|> redirect(to: Helpers.page_path(conn, :index))
这篇关于编程Phoenix:未定义函数page_path/2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!