我正在用 Rails 构建一个 API。我用来构建 api 的 gem 是 Grape 和 Rabl。我已经做了很多工作,但现在我必须status
在来自 api 的所有 json 响应之前添加标志。我怎么能这样做?
我有这个.rabl
文件。
object @current_parent
attributes :first_name => :name
attributes :cell_phone => :mobile
attributes :email
attributes :address
node :day_care do |m|
{
:name => current_day_care.name,
:address => current_day_care.address,
:phone => current_day_care.office_phone,
:website => current_day_care.website,
:logo => current_day_care.logo.url,
:no_of_child => current_parent.relatives.count
}
end
child :relatives, :object_root => false do |m|
child :child, :object_root => false do |n|
attributes :id
attributes :first_name => :name
attributes :gender
attributes :student_stage => :classroom
end
end
这使得以下输出
{
"parent": {
"name": "Devan",
"mobile": "1234567891",
"email": "testparent1@mail.com",
"address": "762 Beahan Expressway",
"day_care": {
"name": "Brisa Erdman",
"address": "859 Hermann Summit",
"phone": "915.758.4580",
"website": "fisher.info",
"logo": "/uploads/day_care/logo/1/http%3A/example.com/ally",
"no_of_child": 2
},
"relatives": [
{
"child": {
"id": 1,
"name": "Lucious",
"gender": "t",
"classroom": "PreKindergarten"
}
},
{
"child": {
"id": 2,
"name": "Lilly",
"gender": "t",
"classroom": "Toddlers"
}
}
]
}
}
但我想在打开status
之前有一个标志,如下所示parent
{
"status": "Success"
"parent": {
"name": "Devan",
"mobile": "1234567891",
"email": "testparent1@mail.com",
但我不知道如何使用 rabl 来实现这一点。请指导我完成这个。如果不可能,请提供替代解决方案。