您需要检查您的
task_view.ex
文件
如果是生成的默认文件
phx.gen.json
看起来像这样:
defmodule MyAppWeb.TaskView do
use MyAppWeb, :view
alias MyAppWeb.TaskView
def render("index.json", %{tasks: tasks}) do
%{data: render_many(tasks, TaskView, "task.json")}
end
def render("show.json", %{task: task}) do
%{data: render_one(task, TaskView, "task.json")}
end
def render("task.json", %{task: task}) do
%{
id: task.id,
title: task.title,
timespent: task.timespent,
details: task.details,
status: task.status
}
end
end
您需要编辑此文件并添加要显示的额外字段。
e、 g.对于
assignee
您可以像这样重用自动生成的视图:
defmodule MyAppWeb.TaskView do
use MyAppWeb, :view
alias MyAppWeb.{TaskView, AsigneeView}
...
def render("task.json", %{task: task} do
%{
id: task.id,
...
asignee: render_one(task.asignee, AsigneeView, "asignee.json"),
}
end
end