How do I render json using Phoenix.View.render_one
? because when I try to render a json view, it result to template not found error. Phoenix tries to render an html instead.
AnnotationView
defmodule RumblWeb.AnnotationView do
use RumblWeb, :view
def render("annotation.json", %{annotation: annotation}) do
%{
id: annotation.id,
body: annotation.body,
at: annotation.at,
user: render_one(annotation.user, RumblWeb.UserSessionView, "user.json")
}
end
end
UserSessionView
defmodule RumblWeb.UserSessionView do
use RumblWeb, :view
def render("user.json", %{user: user}) do
%{id: user.id, name: user.name}
end
end
I use the annotation view, when a user joins a channel:
annotations =
video
|> Multimedia.list_annotations()
|> Phoenix.View.render_many(AnnotationView, "annotation.json")
Here’s the exception:
[error] an exception was raised:
** (Phoenix.Template.UndefinedError) Could not render "user.json" for RumblWeb.UserSessionView, please define a matching clause for render/2 or define a template at "lib/rumbl_web/templates/user_session/*". The following templates were compiled:
* new.html
Assigns:
%{user_session: #Rumbl.Accounts.User<__meta__: #Ecto.Schema.Metadata<:loaded, "users">, confirmed_at: nil, email: "nulloverridedota2@gmail.com", hashed_password: "$2b$12$ugeqoWBcSJO65ZNemMggruOgSqcyA.pvlOJemo0VGTvFWMa19cCuK", id: 2, inserted_at: ~N[2020-05-01 09:23:06], name: "Jhefrey", updated_at: ~N[2020-05-01 09:23:06], videos: #Ecto.Association.NotLoaded<association :videos is not loaded>, ...>, view_module: RumblWeb.UserSessionView, view_template: "user.json"}
Assigned keys: [:user_session, :view_module, :view_template]
If the information is not enough, I will provide more.
Thank you.