wernerlaude

wernerlaude

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.

Showing Posts 1 to 10

mbuhot

mbuhot

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

wernerlaude

wernerlaude OP

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..

acrolink

acrolink

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
benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Can you provide the full error stack trace?

acrolink

acrolink

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)

wernerlaude

wernerlaude OP

thanks.. is in..

wernerlaude

wernerlaude OP

[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..
benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

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

kokolegorille

kokolegorille

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
wernerlaude

wernerlaude OP

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>}}

Where Next? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews