Thursday, 15 March 2012

ruby on rails - Adding custom key-value pair in json output Grape and Rabl -



ruby on rails - Adding custom key-value pair in json output Grape and Rabl -

i building api rails. gems utilize build api grape , rabl. have done lot of work have add together status flag before json response api. how this?

i have .rabl file.

object @current_parent attributes :first_name => :name attributes :cell_phone => :mobile attributes :email attributes :address node :day_care |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 kid :relatives, :object_root => false |m| kid :child, :object_root => false |n| attributes :id attributes :first_name => :name attributes :gender attributes :student_stage => :classroom end end

this makes the next output

{ "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" } } ] } }

but want have status flag in origin before parent opens below

{ "status": "success" "parent": { "name": "devan", "mobile": "1234567891", "email": "testparent1@mail.com",

but cant figure out how create happen using rabl. please guide me through this. if not possible provide alternative solution.

take current parent.rabl file, , utilize partial in new rabl template expresses status.

object false node :parent partial("parent", :object => @parent) end node :status @status end

then phone call rabl file controller

ruby-on-rails api ruby-on-rails-4 rabl grape-api

No comments:

Post a Comment