2013-07-09 65 views
0

我很難讓rabl符合jsonapi.org規範。我在github上看到了關於這個(https://github.com/nesquena/rabl/wiki/Conforming-to-jsonapi.org-format)的wiki條目,但我不明白如何在rabl中獲得多個根級別節點,並且仍然有集合工作。在集合上維基詞條中說這應該工作Rabl符合jsonapi.org

collection [@post] => :posts 
attributes :id, :title 
child @post => :links do 
    node(:author) { @post.author_id } 
    node(:comments) { @post.comments_ids } 
end 

這對於單根文件確實如此,但只要我嘗試將metalinks添加到文檔的根作爲jsonapi.org規範聲明,那些節點被追加到現有的集合節點。這裏是我的rabl_init.rb

require 'rabl' 
Rabl.configure do |config| 
    config.cache_sources = Rails.env != 'development' 
    config.include_child_root = false 
    config.include_json_root = false 
end 

我想json的,看起來像這樣:

{ 
"links": { 
"posts.comments": "http://example.com/posts/{posts.id}/comments" 
}, 
"posts": [{ 
    "id": "1", 
    "title": "Rails is Omakase" 
    }, { 
    "id": "2", 
    "title": "The Parley Letter" 
}] 
} 

是Rabl的能力這樣做?

回答

0

嘗試這樣:

object false 

node(:links) do 
    {"posts.comments" => "http://example.com/posts/{posts.id}/comments"} 
end 

child(@posts) do 
    attributes :id, :title 
end