首页 > 解决方案 > 渲染功能在视图中无法正常工作

问题描述

这是我的控制器代码:

     def index(conn, _params) do
       customers =  Repo.all(Customer)
       render conn, "index.json", customers: customers  
     end

这是客户视图中的代码

 def render("index.json", %{customers: customers}) do
  %{
   data: render_many(customers, CustomerAdminView, "customer.json"),
  }
end 

  def render("customer.json", %{customer: customer}) do   
   %{
     id: customer.id,
     user: customer.user,
     billing_contact: customer.billing_contact

    }
  end

这将引发内部服务器错误。在凤凰视图文档中,我的代码与他们提供的示例相同。如果我将index.json代码更改为此,它会起作用:

def render("index.json", %{customers: customers} = assigns) do
 %{
  data: render_many(customers, CustomerAdminView, 
  "customer.json", assigns),
  }
end   

为什么它适用于 render/4 而不是 render/3?

标签: elixirphoenix

解决方案


推荐阅读