I’m trying to generate JSON from an API controller, it’s configured the following way:
pipeline :api do
plug :accepts, ["json"]
end
# Other scopes may use custom stacks.
scope "/api", ApiWeb do
pipe_through :api
resources "/categories", CategoryController, only: [:index]
end
defmodule Api.CategoryController do
use ApiWeb, :controller
alias Api.Category
def index(conn, _params) do
categories = Frobl.Category.get_all()
render(conn, "index.json", categories: categories)
end
end
And the view…
defmodule Api.CategoryView do
use ApiWeb, :view
def render("index.json", %{categories: categories}) do
render_many(categories, Api.CategoryView, "category.json")
end
def render("category.json", %{ category: category }) do
%{order: category.order, name: category.name, tag: category.tag }
end
end
This always results in:
** (Phoenix.Template.UndefinedError) Could not render “category.json” for ApiWeb.CategoryView, please define a matching clause for render/2 or define a template at"lib/api_web/templates/category". No templates were compiled for this module.
This is quite baffling because I should be matching accordingly, but for some reason it’s not matching on the correct render clause.
Can someone please lend some advice?