1

I've tried a couple different iterations but I keep getting this error when compiling:

Routes.session_path/2 is undefined (module Routes is not available)

My Code:

defmodule Blackbook.Plugs.RequireAuth do
    import Plug.Conn
    import Phoenix.Controller
    alias Blackbook.Router.Helpers, as: Routes

    def init(_params) do
    end

    def call(conn, _params) do
        if conn.assigns[:current_user] do
            conn
        else
            conn
            |> put_flash(:error, "You must be logged in.")
            |> redirect(to: Routes.session_path(conn, :new))
            |> halt()
        end
    end
end

It's a little different in Phoenix 1.4, tried referring to documentation here https://hexdocs.pm/phoenix/Phoenix.Router.html but still no luck.

4

1 回答 1

5

由于您使用的是YourAppWeb ,因此默认情况下它会为与 Web 相关的模块创建一个单独的命名空间。在你的情况下,我相信这只是一个错字,应该是这样的:

alias BlackbookWeb.Router.Helpers, as: Routes

(注意Web部分)

于 2018-11-28T20:02:42.120 回答