Json View problem - Could not render / please define a matching clause for render/2 or define a template

I try to render a view as json.
router:

pipeline :api do
  plug :accepts, ["json"]
end

scope "/api", MyAppWeb do
 pipe_through :api

 get "/networking", PageController, :networking
end

controller:

def networking(conn, _params) do
 q = from m in Meeting, where: m.is_active == true
 meetings = Repo.all(q)
  render(conn, "networking.json", meetings: meetings)
end

view:

def render("networking.json", %{meetings: meetings}) do
 %{data: render_many(meetings, PageView, "networking.json")}
end

def render("networking.json", %{meeting: meeting}) do
   %{id: meeting.id,
   title: meeting.title}
end

But I get: Could not render "networking.json for MyAppWeb.PageView, please define a matching clause for render/2 or define a template…
Did I forget something? Thanks for support.

1 Like

Just a guess, do the controller and view names match?

Think so…

page_controller.ex

defmodule MyAppWeb.PageController do
  use MyAppWeb, :controller

page_view.ex

defmodule MyAppWeb.PageView do
  use MyAppWeb, :view

  alias MyAppWeb.PageView

def render("networking.json", %{meetings: meetings}) do
  %{data: render_many(meetings, PageView, "networking.json")}
end

def render("networking.json", %{meeting: meeting}) do
   %{id: meeting.id,
   title: meeting.title}
end

It is not all clear to me what is happening here, I applied it from a sample…

Make sure you have this inside myapp_web/endpoint.ex file:

  plug Plug.Parsers,
    parsers: [:urlencoded, :multipart, :json],
    pass: ["*/*"],
    json_decoder: Poison

  plug Plug.MethodOverride
  plug Plug.Head

Can you provide the full error stack trace?

I guess the problemscan be solved like this:

def render("index.json", %{meetings: meetings}) do
  %{data: render_many(meetings, PageView, "networking.json")}
end

def render("networking.json", %{meeting: meeting}) do
   %{id: meeting.id,
   title: meeting.title}
end

and in controller:

render(conn, "index.json", meetings: meetings)

1 Like

thanks… is in…

[error] #PID<0.559.0> running MyAppWeb.Endpoint (cowboy_protocol) terminated
Server: localhost:4000 (http)
Request: GET /api/networking
** (exit) an exception was raised:
** (Phoenix.Template.UndefinedError) Could not render “networking.json” for MyAppWeb.PageView, please define a matching clause for render/2 or define a template at “lib/my_app_web/templates/page”. The following templates were compiled:

  • dashboard.html
    etc…

I really did mean the full thing. Do you have a plug accepts json in your router?

The plug is here as it is defined in the router in the OP.

I would try to add a catch all clause in the view, just to see what params are sent. Like so…

def render("networking.json", params) do
   IO.inspect params
end
1 Like

ok… thanks so far
so code is like that now:

router

scope "/api", MyAppWeb do
  pipe_through :api
  get "/networking", PageController, :networking
end

view

defmodule MyAppWeb.PageView do
  use MyAppWeb, :view

  alias MyAppeb.PageView

  def render("networking.json", params) do
   IO.inspect params
  end

page_controller

def networking(conn, _params) do
  q = from m in Meeting, where: m.is_active == true, order_by: m.inserted_at
  meetings = Repo.all(q)
 render(conn, "networking.json", meetings: meetings)
end

error:
Poison.EncodeError at GET /api/networking
unable to encode value: {Plug.Adapters.Cowboy.Conn, {:http_req, #Port<0.23603>, :ranch_tcp, :keepalive, #PID<0.1093.0>, “GET”, :“HTTP/1.1”, {{127, 0, 0, 1}, 57166}, “localhost”, :undefined, 4000, “/api/networking”, :undefined, “”, :undefined, [], [{“host”, “localhost:4000”}, {“connection”, “keep-alive”}, {“upgrade-insecure-requests”, “1”}, {“user-agent”, “Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36”}, {“accept”, “text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,/;q=0.8”}, {“accept-encoding”, “gzip, deflate, br”}, {“accept-language”, “de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7,la;q=0.6”}, {“cookie”, “Webstorm-f9c6b724=b25b909c-75d4-46de-9b31-cded003fdbfa; _ga=GA1.1.424741732.1519378781; Webstorm-f9c6bae3=1e19a62a-70f2-445c-b2dd-b6a425eab18a; axd=1000215891540480040; cookieconsent_dismissed=yes; __utma=111872281.424741732.1519378781.1528479627.1528479627.1; __utmz=111872281.1528479627.1.1.utmcsr=0.0.0.0:3000|utmccn=(referral)|utmcmd=referral|utmcct=/; cookieconsent_status=dismiss; _tiny_houses_net_key=SFMyNTY.g3QAAAACbQAAAAtfY3NyZl90b2tlbm0AAAAYd1pKK05WUmJFNnlScTJ3dllLTWsrQT09bQAAABJwaGF1eHRoX3Nlc3Npb25faWRtAAAAEkZsRXRKdmZSQ2RZNmsxWWZkMQ.lqyimMwpF0DhHsjSA7mJlAKTQyGUz_1weRtsTAZCuzE”}], [{“connection”, [“keep-alive”]}], :undefined, [], :waiting, “”, :undefined, false, :waiting, [], “”, #Function<1.79365245/4 in Plug.Adapters.Cowboy.add_on_response/3>}}

Poison should not be encoding Plug.Adapters.Cowboy.Conn, only meetings object… What is the output of this:

def render("networking.json", %{meetings: meetings}) do
IO.inspect meetings
  %{data: render_many(meetings, PageView, "networking.json")}
end

phoenix.Template.UndefinedError at GET /api/networking
Could not render “networking.json” for MyAppWeb.PageView, please define a matching clause for render/2 or define a template at “lib/tiny_houses_net_web/templates/page”. The following templates were compiled:

I think I have to work through more samples to understand…
Different to Rails…

Did You import Ecto.Query in the controller to use from?

yes…

import Ecto.Query

Did You import Meeting too in the controller?

I guess I would start by checking what is meetings…

IO.inspect meetings

Make sure you have use MyAppWeb, :controller in controller file.

Have you tried my suggestion at post number 5 above?

render(conn, "networking.html", meetings: meetings)

this works fine…
just json not…

I will try to more understand…with some samples…to work through tomorrow
Thanks so far…

Try to regenerate the source files with this:

mix phx.gen.json Accounts User users name:string age:integer

Adjusted for your model needs.

What is your complete router implementation, at the very least the api pipeline?