Render Expects Template to be a String

Hi, I am writing an authentication plug, upon failure I am rendering to 403 error page

    case claims do
      {:ok, claims} ->
        conn
        |> put_status(200)

      _ ->
        conn
        |> render("forbidden.html")
        |> halt()
    end

It gives me this error

** (ArgumentError) render/2 expects template to be a string, got: %Plug.Conn{adapter: {Plug.Cowboy.Conn, :...}, assigns: %{}

I don’t know what I am doing wrong here, Any help would be highly appreciated!

Are you sure you are using correct render/2 function? Especially as in this case you should use put_view/2 or render/3.

1 Like

I don’t know I am thinking it right or not as It is a simple html page I am using render/2, In render/3 I need to pass assign

As I was rendering the user to error page so what I did is create a render function in View and render template there:

  def render("403.html", _assigns) do
    render("forbidden.html", %{})
  end

and in my authentication pipe:

case claims do
  {:ok, claims} ->
    conn
    |> put_status(200)

   _ ->
    conn
    |> put_status(403)
    |> put_view(MyAppWeb.PageView)
    |> render("403.html")
    |> halt()
end
1 Like