从集合中提取 RABL 节点
Extract RABL nodes from collection
我想在我的 JSON 输出中添加一些关于响应的元数据,但 RABL 将它们插入到已处理的集合中。这是我的模板:
views/clients/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}}
此处,status
和 time
信息在 client
集合中。我怎样才能提取它们,以获得以下输出:
{"time":"...","status":200,"client":{...}}
找到了。魔法来自 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
希望对您有所帮助。
我想在我的 JSON 输出中添加一些关于响应的元数据,但 RABL 将它们插入到已处理的集合中。这是我的模板:
views/clients/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}}
此处,status
和 time
信息在 client
集合中。我怎样才能提取它们,以获得以下输出:
{"time":"...","status":200,"client":{...}}
找到了。魔法来自 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
希望对您有所帮助。