Error while rendering generated JSON template

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: post_controller.ex and post_json.ex.

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?

Thanks

What api routes do you have defined in router.ex? And what version of Phoenix are you using?

This looks like your route is trying to access the Phoenix View modules from pre v1.6.

Thanks @03juan. Yes, it looks like my migration to Phoenix 1.7 was only half baked and I’m missing a few configuration functions in myapp_web.ex

1 Like

Completely understand. I’ve been putting off upgrading to LV 0.18 because there are quite a few changes to keep in mind. Luckily it’s a good opportunity to refactor and simplify my views, just need to find the time. Good luck on your upgrade :+1:

1 Like

Did you figure it out what was the missing config? Im running into the same issue.

Found it: I was just missing the changes in the controller function inside your_app_web.ex file.

2 Likes

Being explicit, this fixed it for me in my_app_web.ex:

  def controller do
    quote do
      use Phoenix.Controller,
        namespace: MyAppWeb,
        # New to Phx 1.7
        formats: [:html, :json],
        layouts: [html: MyAppWeb.Layouts]
      ...