Dear kokolegorille,
Thank you very much for responding.
I hope you can help me out and that you can provide a general approach.
The first attempt I made is given below, having a different controller did not change much:
Main error: Could not render “showandu.json” for MijnsiteWeb.PostView (or show.json in case of Postanduser controller)
scope "/api", MijnsiteWeb do
pipe_through :api
resources "/posts", PostController, except: [:new, :edit]
get "/postandusers", PostController, :indexandu
get "/postandusers/:id", PostController, :showandu
end
In post_controller I added:
def indexandu(conn, _params) do
postandusers = Message.list_postandusers()
render(conn, "indexandu.json", postandusers: postandusers)
end
def showandu(conn, %{"id" => id}) do
postanduser = Message.get_postanduser!(id)
render(conn, "showandu.json", postanduser: postanduser)
end
In post_view.ex I added:
def render("indexandu.json", %{postsandusers: postsandusers}) do
%{data: render_many(postsandusers, PostView, "postsanduser.json")}
end
def render("postsanduser.json", %{postsandusers: postsandusers}) do
%{ id: postsandusers.id,
description: postsandusers.description,
complete: postsandusers.complete,
title: postsandusers.title,
body: postsandusers.body,
authorid: postsandusers.authorid}
end
def render("showandu.json", %{postsanduser: postsanduser}) do
%{data: render_one(postsanduser, PostView, "postsanduser.json")}
end
The functions in message.ex are:
def list_postandusers do
query = from p in Post,
join: u in Mijnsite.User, where: u.id == p.authorid,
select: {p.id, p.description, p.complete, p.title, p.body, p.inserted_at, p.updated_at, p.authorid, u.id, u.email, u.familyname, u.firstname}
Repo.all(query)
end
def get_postanduser!(postid) do #: Repo.get!(Postanduser, id)
query = from p in Post,
join: u in Mijnsite.User, where: u.id == p.authorid
# Extend the query
query2 = from [p, u] in query,
select: {p.id, p.description, p.complete, p.title, p.body, p.inserted_at, p.updated_at, p.authorid, u.id, u.email, u.familyname, u.firstname},
where: p.id==^postid
Repo.all(query2)
end
Running http://localhost:4000/api/posts/63 gives:
{"data":{"title":"Ikdoe","id":63,"description":"geen","complete":true,"body":"Lichaam","authorid":1}}`
while running http://localhost:4000/api/postandusers/63 gives:
Could not render "showandu.json" for MijnsiteWeb.PostView, please define a matching clause for render/2 or define a template at "lib/mijnsite_web/templates/post". No templates were compiled for this module.Assigns:`
%{conn: %Plug.Conn{adapter: {Plug.Adapters.Cowboy.Conn, :...}, assigns: %{layout: false, `postanduser: [{63, "geen", true, "Ikdoe", "Lichaam", ~N[2017-11-21 21:45:23.543000], ~N[2017-11-21 21:45:23.543000], 1, 1, "j.val@hccnet.nl", "Val", "John"}]`}, before_send: [#Function<1.66631281/1 in Plug.Logger.call/2>, #Function<0.83265233/1 in Phoenix.LiveReloader.before_send_inject_reloader/2>], body_params: %{}, cookies: %Plug.Conn.Unfetched{aspect: :cookies}, halted: false, host: "localhost", method: "GET", owner: #PID<0.1493.0>, params: %{"id" => "63"}, path_info: ["api", "postandusers", "63"], path_params: %{"id" => "63"}, peer: {{127, 0, 0, 1}, 60334}, port: 4000, private: %{MijnsiteWeb.Router => {[], %{}}, :phoenix_action => :showandu, :phoenix_controller => MijnsiteWeb.PostController, :phoenix_endpoint => MijnsiteWeb.Endpoint, :phoenix_format => "json", :phoenix_layout => {MijnsiteWeb.LayoutView, :app}, :phoenix_pipelines => [:api], :phoenix_router => MijnsiteWeb.Router, :phoenix_template => "showandu.json", :phoenix_view => MijnsiteWeb.PostView, :plug_session_fetch => #Function<1.112984571/1 in Plug.Session.fetch_session/1>}, query_params: %{}, query_string: "", remote_ip: {127, 0, 0, 1}, req_cookies: %Plug.Conn.Unfetched{aspect: :cookies}, req_headers: [{"host", "localhost:4000"}, {"connection", "keep-alive"}, {"user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36"}, {"upgrade-insecure-requests", "1"}, {"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", "nl-NL,nl;q=0.9,en-US;q=0.8,en;q=0.7"}], request_path: "/api/postandusers/63", resp_body: nil, resp_cookies: %{}, resp_headers: [{"cache-control", "max-age=0, private, must-revalidate"}, {"x-request-id", "06pjp8clvu77e6a5p74dvuo95ihoj611"}], scheme: :http, script_name: [], secret_key_base: "EFD0NXPVGy0Cj2QBJkrtXXR1fd+llFOQtARk/VAA6ob4JHMk0ANnYOk2WtYY0Ltd", state: :unset, status: nil}, postanduser: [{63, "geen", true, "Ikdoe", "Lichaam", ~N[2017-11-21 21:45:23.543000], ~N[2017-11-21 21:45:23.543000], 1, 1, "j.val@hccnet.nl", "Val", "John"}], template_not_found: MijnsiteWeb.PostView, view_module: MijnsiteWeb.PostView, view_template: "showandu.json"}
Assigned keys: [:conn, :postanduser, :template_not_found, :view_module, :view_template]