wernerlaude

wernerlaude

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.

Most Liked

pureyoda

pureyoda

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

The root key of the second parameter of the function below must match the view’s name.

views/page_view.ex
def render(“networking.json”, %{page: meeting}) do

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)

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

Where Next?

Popular in Questions Top

aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New

Other popular topics Top

aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 53690 245
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New

We're in Beta

About us Mission Statement