2012-02-21 30 views
6

我有這個Rabl的模板JSON對象,而根密鑰Rabl的:</p> <pre><code>object @photo attributes :id child :comments do attributes :id, :body end </code></pre> <p>這給了我這個JSON響應:

{ 
    photo: { 
    id: 1, 
    comments: [ 
     { 
     comment: { 
      id: 1, 
      body: 'some comment' 
     } 
     }, 
     { 
     comment: { 
      id: 2, 
      body: 'another comment' 
     } 
     } 
    ] 
    } 
} 

但我希望它看起來像這樣:

{ 
    id: 1, 
    comments: [ 
    { 
     id: 1, 
     body: 'some comment' 
    }, 
    { 
     id: 2, 
     body: 'another comment' 
    } 
    ] 
} 

爲什麼rabl會用一個名爲comment的額外對象來包裹陣列中的每個元素。這樣,當我訪問集合中的JavaScript我有寫:的

var comment = image.comments[0].comment 

代替:

var comment = image.comments[0] 

我知道,如果我包括在屬性列表中@photo對象:comments它的工作原理我想要的方式,但是當我想要每個comment對象的另一級嵌套關聯時,除了使用child之外,沒有辦法處理它,但是這給了我不想要的JSON響應。

也許我只是誤解了整個事情 - 有人可以解釋或幫助嗎?謝謝!

回答

7

Got it!

創建config/initializers/rabl_config.rb一個新的文件:

Rabl.configure do |config| 
    config.include_json_root = false 
    config.include_child_root = false 

end 
+2

我有同樣的問題,我發現我需要添加「config.include_child_root =假」除了include_json_root = FALSE – 2012-10-21 19:04:59

相關問題