2014-06-18 5 views
1

레일스로 API를 구축 중입니다. 내가 API를 만드는 데 사용하는 보석은 포도와 Rabl입니다. 나는 많은 일을했지만 지금은 API에서 모든 json 응답 앞에 status 플래그를 추가해야합니다. 내가 어떻게 할 수 있니?json 출력에 맞춤 키 - 값 쌍 추가하기 포도 및 Rabl

나는이 .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": "[email protected]", 
    "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" 
      } 
     } 
    ] 
} 
} 

하게하지만 parent 아래

{ 
"status": "Success" 
"parent": { 
    "name": "Devan", 
    "mobile": "1234567891", 
    "email": "[email protected]", 

처럼 열립니다하지만 난 방법을 알아낼 질수 전에 나는 처음에 status 플래그를 갖고 싶어 이것이 rabl을 사용하여 일어나는 것. 제발 이걸 안내 해줘. 가능한 경우 대체 솔루션을 제공하십시오.

답변

1

파일을 현재 parent.rabl 파일로 가져 와서 상태를 나타내는 새로운 rabl 템플릿의 일부로 사용하십시오.

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

그러면 대답을 컨트롤러에서

+0

이것은 작동합니다. 고마워요. 나는 또 다른 방법을 찾아 냈다. 내 대답도 올릴거야. – mgs

0

감사 곤드레 만드레 취한를이 rabl 파일을 호출합니다. 내 문제에 대한 또 다른 해결책을 알아 냈어. 그리고 그것은 내 상황 즉, 여분의 파일을 만들고 원래로 parent.rabl을 부분적으로 호출 할 필요가없는 것으로 보인다. 이 방법으로 내보기 파일 수는 동일합니다. 내가 원하는 결과를 얻을 이런 방식으로 살펴

솔루션 parent.rabl 파일

node do |u| 
{ 
    :status => "Success" 
} 
end 
child @current_parent do 

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 
end 

있습니다.