Ruby on rails 加上「;名称空间;到序列化程序
我的数据库中存在Ruby on rails 加上「;名称空间;到序列化程序,ruby-on-rails,ruby,active-model-serializers,serialization,Ruby On Rails,Ruby,Active Model Serializers,Serialization,我的数据库中存在Usermodel,但是我想用active\u model\u序列化程序返回json响应,其中User属性被封装/嵌套在player命名空间中,而该命名空间在数据库中不存在(假设它是虚拟的,这是任意的预期响应) 而不是: { "email": "some@mail.com", "first_name": "Hey", "last_name": "Hoo", "birthdate": "1540-05-05", "phone_number":
User
model,但是我想用active\u model\u序列化程序返回json响应,其中User
属性被封装/嵌套在player
命名空间中,而该命名空间在数据库中不存在(假设它是虚拟的,这是任意的预期响应)
而不是:
{
"email": "some@mail.com",
"first_name": "Hey",
"last_name": "Hoo",
"birthdate": "1540-05-05",
"phone_number": "856539571"
}
我希望:
{
"player":
{
"email": "some@mail.com",
"first_name": "Hey",
"last_name": "Hoo",
"birthdate": "1540-05-05",
"phone_number": "856539571"
}
}
在UserSerializer类中,定义根属性。例如:
class UserSerializer < ActiveModel::Serializer
root :player
...
end
class UserSerializer
在UserSerializer类中,定义根属性。例如:
class UserSerializer < ActiveModel::Serializer
root :player
...
end
class UserSerializer
当Nwocha的答案正确时,我将添加更多细节
至于说:
重写资源根仅在使用JSON适配器时适用
通常,资源根是从正在序列化的资源的类名派生的。e、 g.UserPostSerializer.new(UserPost.new)
将根据适配器集合的多重化规则,使用根user\u post
或user\u posts
进行序列化
在初始值设定项(ActiveModelSerializers.config.adapter=:JSON
)中使用JSON适配器或在呈现调用中传递适配器时,可以通过将其作为要呈现的参数传递来指定根。例如:
呈现json:@user\u post,root:“admin\u post”,适配器::json
这将呈现为:
{
“管理员职位”:{
“标题”:“如何做开源”
}
}
注意:属性适配器(默认)不包括资源根。如果使用:json_api适配器,您也将无法创建单个顶级根。当Nwocha的答案正确时,我将为其添加更多详细信息 至于说: 重写资源根仅在使用JSON适配器时适用 通常,资源根是从正在序列化的资源的类名派生的。e、 g.
UserPostSerializer.new(UserPost.new)
将根据适配器集合的多重化规则,使用根user\u post
或user\u posts
进行序列化
在初始值设定项(ActiveModelSerializers.config.adapter=:JSON
)中使用JSON适配器或在呈现调用中传递适配器时,可以通过将其作为要呈现的参数传递来指定根。例如:
呈现json:@user\u post,root:“admin\u post”,适配器::json
这将呈现为:
{
“管理员职位”:{
“标题”:“如何做开源”
}
}
注意:属性适配器(默认)不包括资源根。如果使用:json_api适配器,您也将无法创建单个顶级根。之前提供的答案也可以,但最后我使用了稍微不同的方法。结果是除了
player
之外,应该还有另一个json,比如说team
,这使得最终的响应看起来像这样:
{
"player":
{
"email": "some@mail.com",
...
},
"team":
{
"name": "Fancy Team",
...
}
}
class UserSerializer < ActiveRecord::Serializer
attributes :player
attributes :team
def player
{
email: object.email,
...
}
end
def team
{
name: object.name,
...
}
end
end
我实际做的是定义我想要使用的确切json,如下所示:
{
"player":
{
"email": "some@mail.com",
...
},
"team":
{
"name": "Fancy Team",
...
}
}
class UserSerializer < ActiveRecord::Serializer
attributes :player
attributes :team
def player
{
email: object.email,
...
}
end
def team
{
name: object.name,
...
}
end
end
class UserSerializer
如果我在
renderjson:
中使用root
选项,则整个序列化程序将封装在这个名称中。很抱歉没有在开始时清除它。之前提供的答案同样有效,但最后我使用了稍微不同的方法。结果是除了player
之外,应该还有另一个json,比如说team
,这使得最终的响应看起来像这样:
{
"player":
{
"email": "some@mail.com",
...
},
"team":
{
"name": "Fancy Team",
...
}
}
class UserSerializer < ActiveRecord::Serializer
attributes :player
attributes :team
def player
{
email: object.email,
...
}
end
def team
{
name: object.name,
...
}
end
end
我实际做的是定义我想要使用的确切json,如下所示:
{
"player":
{
"email": "some@mail.com",
...
},
"team":
{
"name": "Fancy Team",
...
}
}
class UserSerializer < ActiveRecord::Serializer
attributes :player
attributes :team
def player
{
email: object.email,
...
}
end
def team
{
name: object.name,
...
}
end
end
class UserSerializer
如果我在
renderjson:
中使用root
选项,则整个序列化程序将封装在这个名称中。很抱歉没有在开始时清除它。可能是查看,查看nests\u one
并在序列化程序中使用它。可能是查看,查看nests\u one
并在序列化程序中使用它。我的UserSerializer
定义与您的定义完全相同,但是当我调用它时,它将undefined\u方法
抛出到root
上。您使用的是什么版本的AMS?在这个项目中,它是active\u model\u序列化程序(0.10.10)
我已经像您一样定义了UserSerializer
,但是当我调用它时,它会在根目录上抛出undefined\u方法
。您使用的是AMS的哪个版本?在这个项目中,它是active\u model\u序列化程序(0.10.10)