elixir – 在Phoenix Framework中渲染多对多关系JSON

栏目: 编程语言 · 发布时间: 7年前

内容简介:翻译自:https://stackoverflow.com/questions/34750899/render-many-to-many-relationship-json-in-phoenix-framework

我有下一个型号

defmodule App.User do
  use App.Web, :model
  alias App.User

  schema "users" do  
    field :name, :string
    has_many :roles_users, App.RolesUser
    has_many :roles, through: [:roles_users, :role]
    timestamps
  end
end 

defmodule App.Role do
  use App.Web, :model

  schema "roles" do
    has_many :roles_users, App.RolesUser
    has_many :users, through: [:roles_users, :user]
    field :name, :string
    timestamps
  end

end

defmodule App.RolesUser do
  use App.Web, :model

  schema "roles_users" do
    belongs_to :role, App.Role
    belongs_to :user, App.User
    timestamps
  end
end

是为了多对多的关系.我要显示的控制器是

def index(conn, _params) do
  users = Repo.all(User)
        |> Repo.preload(:roles)

  render(conn, "index.json", users: users)
end

在我看来

def render("index.json", %{users: users}) do
  %{users: render_many(users, App.UserView, "user.json")}
end

def render("show.json", %{user: user}) do
  %{user: render_one(user, App.UserView, "user.json")}
end

def render("user.json", %{user: user}) do
  %{id: user.id,
    name: user.name,
    roles: user.roles
 }

当我发送GET请求时,我收到了此错误

unable to encode value: {nil, "roles"}

我知道这可能是因为user.roles需要以某种方式格式化以解码JSON,但我对此没有任何线索.我在表格中试过了

def render("user.json", %{user: user}) do
  %{id: user.id,
    name: user.name,
    roles: render_many(roles, App.UserView, "roles.json")
 }

但是没有用.

在视图中渲染多对多关系的最佳方法是什么?

使用 render_many/4

是正确的.

如果您希望在同一模块中定义“role.json”渲染功能,您可以执行以下操作:

def render("user.json", %{user: user}) do
  %{
    id: user.id,
    name: user.name,
    roles: render_many(user.roles, __MODULE__, "role.json", as: :role)
  }
end

def render("role.json", %{role: role}) do
  %{
    id: role.id
    ... 
  }
end

请注意,我们将:: role传递给render_many函数.这是因为从视图名称推断出分配(%{role:role})部分.在这种情况下,它是UserView,因此默认情况下它是%{user:user}.

如果您定义了RoleView模块,那么您只需将def render(“role.json”)函数移动到新的RoleView并调用render_many而不使用as选项:

...
roles: render_many(user.roles, MyApp.RoleView, "role.json")
...

您可能更喜欢的另一个选项是在模型中派生协议:

defmodule App.Role do
  use App.Web, :model
  @derive {Poison.Encoder, only: [:id, :name]}

  schema "roles" do
    has_many :roles_users, App.RolesUser
    has_many :users, through: [:roles_users, :user]
    field :name, :string
    timestamps
  end

就个人而言,我认为这会将您的模型与您的视图相结合,所以我更喜欢使用第一个选项.

翻译自:https://stackoverflow.com/questions/34750899/render-many-to-many-relationship-json-in-phoenix-framework


以上所述就是小编给大家介绍的《elixir – 在Phoenix Framework中渲染多对多关系JSON》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Types and Programming Languages

Types and Programming Languages

Benjamin C. Pierce / The MIT Press / 2002-2-1 / USD 95.00

A type system is a syntactic method for automatically checking the absence of certain erroneous behaviors by classifying program phrases according to the kinds of values they compute. The study of typ......一起来看看 《Types and Programming Languages》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具