我失去了一些基本的东西与我的凤凰代码药剂地图改变结构周围
这是在我的控制器
def show(conn, %{"id" => id}) do
user = Repo.get!(User, id)
query =
from c in Card,
where: c.user_id == 1,
select: {c.id, c.inserted_at, c.estimate_name, c.product}
estimates = Repo.all(query)
render(conn, "show.json", %{user: user, estimates: estimates})
# render(conn, "show.json", user: user)
end
在我看来
# def render("show.json", %{user: user}) do
def render("show.json", %{user: user, estimates: estimates}) do
%{data: render_one(%{user: user, estimates: estimates}, Api.UserView, "user.json") }
# %{data: render_one(user, Api.UserView, "user.json")}
# ** New code since original post **
# %{data: render("user.json", %{user: user, estimates: estimates})}
end
def render("user.json", %{user: user, estimates: estimates}) do
# def render("user.json", %{user: user}) do
%{id: user.id,
firstname: user.firstname,
lastname: user.lastname,
email: user.email,
customerId: user.customerId,
estimates: render("estimates.json", estimates)} # **Line with last error**
end
def render("estimates.json", [head | _estimates]) do
# Enum.map(estimates, fn estimate -> render(mapper estimate, MosaicApi.UserView, "summaryEstimate.json") } end)
# render(mapper(head), MosaicApi.UserView, "summaryEstimate.json")
render("summaryEstimate.json", mapper(head))
end
# ** I'm fear I have added unnecessary complexity here **
def mapper({id, date, name, product}) do
%{id: id,
creation_date: date,
estimate_name: name,
product: product}
end
def render("summaryEstimate.json", estimate) do
%{id: estimate.id,
estimate_name: estimate.estimate_name,
product: estimate.product}
end
,但我得到通过一个错误,这似乎是因为我的数据已经从%{user: ..., estimates: [...]}
变为%{user: %{estimates:[...], user: %{...}}
。
这是怎么发生的,我该如何预防呢?
Could not render "user.json" for MosaicApi.UserView, please define a matching clause for render/2 or define a template at "web/templates/user". No templates were compiled for this module.
Assigns:
%{user: %{estimates: [{1, #Ecto.DateTime<2016-04-26T12:01:34Z>, "cards for annabelle", "Business Cards"}, ...], user: %Api.User{__meta__: #Ecto.Schema.Metadata<:loaded>, customerId: "CUST", email: "[email protected]", firstname: "fname 1", id: 1, inserted_at: #Ecto.DateTime<2016-04-26T11:46:21Z>, jobs: #Ecto.Association.NotLoaded<association :jobs is not loaded>, lastname: "lname 1", updated_at: #Ecto.DateTime<2016-04-26T11:46:21Z>}}, view_module: Api.UserView, view_template: "user.json"}
这使一个很大的区别,但现在我得到'参数错误 (STDLIB):maps.from_list([{1,#Ec。我已经添加了一些代码,我认为我的查询提供了一个列表,并且我可以对该列表进行模式匹配,但是不确定事实上是这种情况 –
看起来你有点偏离轨道,检查我的编辑 – Gazler
感谢!!看起来你有一个错字,因为它最终需要'估计:render_many(估计,__MODULE__,“estimated.json”,作为::估计)}' –