从集合中提取RABL节点

时间:2015-04-16 11:53:56

标签: ruby-on-rails json rabl

我想在我的JSON输出中添加一些关于响应的元数据,但是RABL将它们插入到处理过的集合中。这是我的模板:

视图/客户端/ show.rabl

node(:status) { response.status }
node(:time) { Time.now }

collection @client
attributes :name, :base_url, :city
attribute c_at: :created_at

node(:items_count) { |c| c.items.count }
node(:users_count) { |c| c.users.count }

这给了我以下JSON响应:

{"client":{"status":200,"time":"2015-04-16 13:45:46 +0200","name":"This is a test","base_url":"http://test.com","city":"Paris","created_at":"2015-04-15 10:38:44 UTC","items_count":3,"users_count":0}}

此处,statustime信息位于client集合中。如何提取它们,以获得以下输出:

{"time":"...","status":200,"client":{...}}

1 个答案:

答案 0 :(得分:0)

找到它。魔术伴随着object false。这是答案。

object false

node(:status) { response.status }
node(:time) { Time.now }

child @client do
  attributes :name, :base_url, :city
  attribute c_at: :created_at

  node(:items_count) { |c| c.items.count }
  node(:users_count) { |c| c.users.count }
end

希望它会有所帮助。