2012-01-17 30 views
1

創建自定義的子節點,我想這樣的創建JSON:與Rabl的

{ 
    "field1": "value-1", 
    "field2": "value-2", 
    "actions": { 
    "edit": "/edit/action/url" 
    } 
} 

使用Rabl的。它不適用於child方法,我也不能用node做 - 它有未知的方法錯誤。那麼,是否有可能使用rabl模板創建自定義子節點?說是這樣的:

collection @datas => :datas 
attributes :field1, :field2 
child :actions do 
    node :edit do 
    edit_datas_path :id 
    end 
end 

編輯
我選擇這一項:

node :actions do 
    actions = {} 
    actions[:edit] = edit_customer_group_path(:id) 
    node :foo do 
    foos = {} 
    foos[:bar] = "hello" 
    foos 
    end 
    actions 
end 

,但低於接受的答案是正確的了。

回答

13

我剛在前一天偶然發現了這個問題。這在RABL問題ticket幫助了我。在你的情況,我是能夠產生正確使用JSON:

collection @datas 
extends "datas/base" 
node :actions do 
    {:edit => root_path} 
end 

產生JSON:

{ 
    id: 1, 
    actions: { 
      edit: "/" 
      } 
} 

使用Rabl的0.5.3

+0

啊...我發現了類似的解決方案。我會發佈一個答案... – NilColor 2012-01-18 11:24:58