【问题标题】:Rabl conforming to jsonapi.orgRabl 符合 jsonapi.org
【发布时间】:2013-07-09 13:14:39
【问题描述】:

我很难让 rabl 符合 jsonapi.org 规范。我已经在 github 上看到了关于此的 wiki 条目(https://github.com/nesquena/rabl/wiki/Conforming-to-jsonapi.org-format),但我不明白如何在 rabl 中获取多个根级节点并且仍然可以使用集合。集合上的 wiki 条目说这应该可以工作

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 有能力做到这一点吗?

【问题讨论】:

    标签: ruby-on-rails json rabl


    【解决方案1】:

    试试这样的:

    object false
    
    node(:links) do
      {"posts.comments" => "http://example.com/posts/{posts.id}/comments"}
    end
    
    child(@posts) do
      attributes :id, :title
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-27
      相关资源
      最近更新 更多