I have a
phoenix app that produces both HTML and JSON outputs via different views. Recently, I decided to use
phx.gen.json to scaffold a new resource. Everything that was generated looked familiar apart from the JSON template. The template is colocated with the controller: e.g. if the resource is named
Post, there will be two added modules in the controllers directory:
The code inside
post_json.ex looked more concise than the typical
post_view I normally would have in the views directory. However, when making requests against an endpoint in the controller I end up with an error:
** (exit) an exception was raised: ** (ArgumentError) no "index" json template defined for MyApp.CurriculumView (phoenix_template 1.0.1) lib/phoenix/template.ex:241: Phoenix.Template.render_with_fallback/4 (phoenix_template 1.0.1) lib/phoenix/template.ex:126: Phoenix.Template.render_to_iodata/4 (phoenix 1.7.2) lib/phoenix/controller.ex:1010: anonymous fn/5 in
Given the modules are all generated, it makes no sense that Phoenix cannot locate the colocated JSON template?
The code I have is nearly identical to the documentation here; only names are different. Is there a piece of configuration I’m missing?