我使用"mix phoenix.gen.json"来生成渲染json的代码,结果如下所示:
defmodule Pghm.SightingsView do
use Pghm.Web, :view
def render("sighting.json", %{sighting: sighting}) do
%{what: sighting.what,
lat: sighting.lat,
long: sighting.long}
end
def render("index.json", %{sightings: sightings}) do
%{data: render_many(sightings, Pghm.SightingsView, "sighting.json")}
end
def render("show.json", %{sighting: sighting}) do
%{data: render_one(sighting, Pghm.SightingsView, "sighting.json")}
end
end
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试访问它时,我收到:无法为Pghm.SightingsView渲染"sighting.json",请为render/2定义匹配子句或在"web/templates/sightings"中定义模板.没有为此模块编译模板.分配:
我看过的每个地方都表明这应该有效,但我没有爱.