View.render/2 is undefined & .render/2 conflicts with local function

i have 2 controller :

page_controller.ex :

defmodule ColaWeb.PageController do
  use ColaWeb, :controller

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

my_controller.ex

defmodule ColaWeb.MyController do
  use ColaWeb, :controller

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

  def laugh(conn, _params) do
    conn |> json(%{"ok": 1}) # OK
  end
end

Error :

function ColaWeb.MyView.render/2 is undefined (module ColaWeb.MyView is not available)

then i change my my_controller.ex :

defmodule ColaWeb.MyController do
  use ColaWeb, :controller
  use ColaWeb, :view

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

  def laugh(conn, _params) do
    conn |> json(%{"ok": 1}) # OK
  end
end

error :

lib/cola_web/controllers/my_controller.ex:1: imported Phoenix.Controller.render/2 conflicts with local function

:wave:

To solve your first error for

defmodule ColaWeb.MyController do
  use ColaWeb, :controller

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

  def laugh(conn, _params) do
    conn |> json(%{"ok": 1}) # OK
  end
end

you need to create a views/my_view.ex and templates/my/index.html.eex.

views/my_view.ex can be as simple as:

defmodule ColaWeb.MyView do
  use ColaWeb, :view
end
2 Likes

yes, i forgot views/my_view.ex