when controller debuging, error "Endpoint.path/1 is undefined (module Endpoint is not available)" appeared

Advertisements

When trying to manual rending, the following error message is received

    (phoenix_view 1.1.2) Phoenix.View.render(PenWeb.LoginView, "show.html")
iex(21)> Phoenix.View.render(PenWeb.LoginView, "show.html", message: "Hello from IEx!") 
%Phoenix.LiveView.Rendered{
  dynamic: #Function<0.19280611/1 in PenWeb.LoginView."show.html"/1>,
  fingerprint: 131643824716591738070856747524267661638,
  root: false,
  static: ["<h2>Log in</h2>\n", ""]
}
iex(22)> Phoenix.View.render_to_string(PenWeb.LoginView, "show.html", message: "Hello from IEx!")
** (UndefinedFunctionError) function Endpoint.path/1 is undefined (module Endpoint is not available)
    Endpoint.path("/login")
    lib/pen_web/templates/login/show.html.heex:2: anonymous fn/2 in PenWeb.LoginView."show.html"/1
    (phoenix_live_view 0.17.11) lib/phoenix_live_view/engine.ex:124: Phoenix.HTML.Safe.Phoenix.LiveView.Rendered.to_iodata/1
    (phoenix_view 1.1.2) lib/phoenix/view.ex:482: Phoenix.View.render_to_string/3

show.html.heex is as follows:

<h2>Log in</h2>
<%= form_tag(Routes.login_path(Endpoint, :create)) do %>
  <div class="form-group">
    <label for="username">Username or Email</label>
    <input id="username" type="text" class="form-control" name="username">
  </div>

  <div class="form-group">
    <label for="password">Password</label>
    <!--span style="float: right;"><a href="<%= Routes.password_reset_path(Endpoint, :show) %>">Forgot your password?</a></span-->
    <input id="password" type="password" class="form-control" name="password">
  </div>

  <!--input type="hidden" name="return" value="<%= @return %>"-->

  <button type="submit" class="btn btn-primary">Log in</button>

  <br><br>

<% end %>

login_view.ex is as follows:

defmodule PenWeb.LoginView do
  use PenWeb, :view
end

routes is as follows:

macbook:pen yuchen$ mix phx.routes
          page_path  GET   /                                      PenWeb.PageController :index
         login_path  GET   /login                                 PenWeb.LoginController :show
         login_path  POST  /login                                 PenWeb.LoginController :create
         login_path  POST  /logout                                PenWeb.LoginController :delete
live_dashboard_path  GET   /dashboard                             Phoenix.LiveDashboard.PageLive :home
live_dashboard_path  GET   /dashboard/:page                       Phoenix.LiveDashboard.PageLive :page
live_dashboard_path  GET   /dashboard/:node/:page                 Phoenix.LiveDashboard.PageLive :page
                     *     /dev/mailbox                           Plug.Swoosh.MailboxPreview []

PenWeb’s view function is as follows:

  def view do
    quote do
      use Phoenix.View,
        root: "lib/pen_web/templates",
        namespace: PenWeb

      # Import convenience functions from controllers
      import Phoenix.Controller,
        only: [get_flash: 1, get_flash: 2, view_module: 1, view_template: 1]

      # Include shared imports and aliases for views
      unquote(view_helpers())
    end
  end

view_helpers function is as follows:

  defp view_helpers do
    quote do
      # Use all HTML functionality (forms, tags, etc)
      use Phoenix.HTML

      # Import LiveView and .heex helpers (live_render, live_patch, <.form>, etc)
      import Phoenix.LiveView.Helpers

      # Import basic rendering functionality (render, render_layout, etc)
      import Phoenix.View

      import PenWeb.ErrorHelpers
      import PenWeb.Gettext
      alias PenWeb.Router.Helpers, as: Routes
    end

end

>Solution :

The Endpoint is not aliased in your view, which mean that template takes "top level" module Endpoint which do not exists. However inside the templates you should use @conn which is Plug.Conn passed from the controller to the view and can be used in exactly the same place as endpoint module.

Leave a ReplyCancel reply