ErrorView.render/2 is undefined

I made a check_admin Plug to check if user’s is_admin is equal to true. If so the user can access the page otherwise he gets a 404 error.

check_admin.ex:

defmodule Gazette.CheckAdmin do
  import Phoenix.Controller
  import Plug.Conn

  def init(opts), do: opts
  def call(conn, _opts) do
    current_user = Guardian.Plug.current_resource(conn)
    if current_user.is_admin do
      conn
    else
      conn
      |> put_status(:not_found)
      |> render(Gazette.ErrorView, "404.html")
      |> halt
    end
  end
end

this does not work when a non-admin user tries to get to page he is not supposed to; Hence the check_admin.ex Plug tries to redirect this user to 404.html but when it get to the error view I get the following error:

function Gazette.ErrorView.render/2 is undefined (module Gazette.ErrorView is not available)

error_view.ex:

 def render("404.html", _assigns) do
    "Page not found"
  end

any idea ?

what is the module name in the error_view.ex file? (eg post the top of the file…)

defmodule GazetteWeb.ErrorView do
  use GazetteWeb, :view

[…]

vs [quote=“JulienCorb, post:3, topic:12418”]
defmodule GazetteWeb.ErrorView do
[/quote]

so probably you want to change the render function to:

|> render(GazetteWeb.ErrorView, "404.html")

4 Likes

function Gazette.ErrorView.render/2 is undefined, indeed. You’ve defined GazetteWeb.ErrorView.render/2. Notice Web

2 Likes

Indeed, was following a 1.2 tutorial… Thank you guys ! you rock

1 Like