Ruby on rails 3.2 自定义序列化程序的输出
所以我想使用最近添加到rails中的新的Ruby on rails 3.2 自定义序列化程序的输出,ruby-on-rails-3.2,active-model-serializers,Ruby On Rails 3.2,Active Model Serializers,所以我想使用最近添加到rails中的新的[ActiveModel::Serializer][1] 如果我这样编写序列化程序: class PatientSerializer < ActiveModel::Serializer attributes :patient_money, :doctor_name def doctor_name object.doctor.name end end [ // HERE.... { "patient_mone
[ActiveModel::Serializer][1]
如果我这样编写序列化程序:
class PatientSerializer < ActiveModel::Serializer
attributes :patient_money, :doctor_name
def doctor_name
object.doctor.name
end
end
[
// HERE....
{
"patient_money":"40.0",
"doctor_name":"Ostad"
},
{
"patient_money":"30.0",
"doctor_name":"Oz"
},
{
"patient_money":"20.0",
"doctor_name":"Phil"
}
]
这与我想要的非常接近,但是在我上面的“这里
”注释中,我想要一个JSON的包装层,因此我可以说,例如my_results
,然后将这三个JSON结果像数组一样包装起来,就像我们在JBuilder中创建新元素时说的json.my_results
好的,我知道了。
在发送带有(@blah)的响应的控制器中我应该传递一个根元素,并将其命名为我喜欢的名称,例如:
respond_with(@blah, root: 'my_results')