问题描述
我有下一个型号
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
是多对多关系.我要显示的控制器是
Is for a many to many relationship. My controller to show is
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请求时出现此错误
When I sent the GET request I got this error
unable to encode value: {nil, "roles"}
我知道这可能是因为user.roles
需要以某种方式格式化才能解码JSON,但是我对此一无所知.我尝试过这种形式
I know that could be because user.roles
need to be formatted in some way to decode the JSON, but I have not any clue about this. I have tried in the form
def render("user.json", %{user: user}) do
%{id: user.id,
name: user.name,
roles: render_many(roles, App.UserView, "roles.json")
}
但是不起作用.
在视图中呈现多对多关系的最佳方法是什么?
What is the best way to render many to many relationships in view?
推荐答案
使用 render_many/4 是正确的.
如果您希望在同一模块中定义"role.json"渲染函数,则可以执行以下操作:
If you wish to define the "role.json" render function in the same module you can do:
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
请注意,我们将as: :role
传递给render_many函数.这是因为从视图名称推断出assign(%{role: role}
)部分.在这种情况下,它是UserView
,因此默认情况下为%{user: user}
.
Notice that we pass as: :role
to the render_many function. This is because the assigns (the %{role: role}
) part is inferred from the view name. In this case it is the UserView
so it would be %{user: user}
by default.
如果定义了RoleView
模块,则只需将def render("role.json")
函数移至新的RoleView
并调用render_many
而不使用as
选项:
If you define a RoleView
module then you can just move the def render("role.json")
function to your new RoleView
and call render_many
without the as
option:
...
roles: render_many(user.roles, MyApp.RoleView, "role.json")
...
您可能更喜欢的另一种选择是在模型中导出协议:
Another option that may be preferable for you is to derive a protocol in your model:
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
我个人认为这会将您的模型与您的视图结合起来,所以我更喜欢使用第一个选项.
Personally I feel this couples your model to your view, so I prefer to use the first option.
这篇关于在Phoenix Framework中呈现多对多关系JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!