Undefined function session_path/2

phoenix
routes
troubleshooting

#1

Hi,

I am having trouble debugging an error:

== Compilation error in file lib/booking_central_web/views/session_view.ex ==
** (CompileError) lib/booking_central_web/templates/session/register.html.eex:9: undefined function session_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:198: anonymous fn/4 in Kernel.ParallelCompiler.spawn_workers/6

Here is my router:

    scope "/", BookingCentralWeb do
    pipe_through :browser # Use the default browser stack

    get "/", DashboardController, :index
    
    get "/login", SessionController, :login_form
    get "/register", SessionController, :register_form
    post "/register", SessionController, :register_user
    end

I have a controller:

defmodule BookingCentralWeb.SessionController do
  
  use BookingCentralWeb, :controller

  alias BookingCentral.Accounts.{Registration}

  plug :put_layout, "accounts.html"

    def login_form(conn, _params) do
        render conn, "login.html"
    end

    def register_form(conn, _params) do
        changeset = Registration.changeset(%Registration{})
        render conn, "register.html", changeset: changeset
    end

    def register_user(conn, %{"registration_params" => params}) do
        IO.inspect params
        render conn, "login.html"
    end

end

Here is my View:

defmodule BookingCentralWeb.SessionView do
  use BookingCentralWeb, :view
end

And I am referencing the session_path in my form:

<%= form_for @changeset, session_path(@conn, :register_user), fn f -> %>

Any suggestions for working out why I get this error are appreciated.

Andrew


#2

The error says there is no session_path, which is the case…

Which line in your router makes You think it is?

You might confirm with

$ mix phx.routes

#3

Are you using Phoenix 1.4 by chance? You need to use Routes.session_path() if you are.


#4
get "/login", SessionController, :login_form
get "/register", SessionController, :register_form
post "/register", SessionController, :register_user

All three of those should define session_path in 1.3


#5

Thanks … this is it!


#6

That is a bit embarassing :slight_smile:

Sorry for misleading answer


#7

All good. I had seen this change … but forgot all about it!


#8

Thank you so much, I did not know that. Could tell me what it has changed?


#9

There is a new alias Routes for all of the generated paths