呈现一个节点在rabl称为属性 - Render a node called attributes in rabl

- 此内容更新于:2016-02-01
主题:

我使用rabl呈现一个jsonapi的回应。根据jsonapi文档结构http://jsonapi.org/format/文档结构我需要一个属性节点,我像后,这是我rabl模板代码;在呈现rabl视为一个关键字和递归解析用户对象属性和属性,包括json如。我怎么能呈现一个节点,只包括和?

原文:

I am using rabl to render a json api response.

As per the json api document structure http://jsonapi.org/format/#document-structure I need an attributes node, I'm after something like;

"data": {
  "type": "users",
  "id": "5",
  "attributes": {
    "full-name": "John Citizen",
    "email": "john.citizen@citizen.id"
  }
}

This is my rabl template code;

object @user => :data
node(:type) { "users" }
node(:id) { |user| user.id.to_s }
node(:attributes) do |user|
  node(:full_name) { |user| user.full_name }
  node(:email) { |user| user.email }
end

When rendering rabl sees :attributes as a keyword and recursively parses the user object attributes and properties and includes those in the json e.g.

{
  "data":
  {
    "type":"users","id":"5",
    "attributes":[
      {"name":"type","options":{},"block":{}},
      {"name":"id","options":{},"block":{}},
      {"name":"attributes","options":{},"block":{}},
      {"name":"full_name","options":{},"block":{}},
      {"name":"email","options":{},"block":{}}
    ],
    "full-name":"John Citizen",
    "email":"john.citizen@citizen.id"
  }
}

How can I render a node called attributes and include only the full_name and email?