Sha256: f1980fce2be5af118ca44e23e795213269817673a555947a51ef68942dcf63be

Contents?: true

Size: 1.04 KB

Versions: 12

Compression:

Stored size: 1.04 KB

Contents

# How to add root key

Add the root key to your API is quite simple with AMS. The **Adapter** is what determines the format of your JSON response. The default adapter is the ```FlattenJSON``` which doesn't have the root key, so your response is something similar to:

```json
{
  "id": 1,
  "title": "Awesome Post Tile",
  "content": "Post content"
}
```

In order to add the root key you need to use the ```JSON``` Adapter, you can change this in an initializer:

```ruby
ActiveModel::Serializer.config.adapter = :json
```

You can also specify a class as adapter, as long as it complies with the AMS adapters interface.
It will add the root key to all your serialized endpoints.

ex:

```json
{
  "post": {
    "id": 1,
    "title": "Awesome Post Tile",
    "content": "Post content"
  }
}
```

or if it returns a collection:

```json
{
  "posts": [
    {
      "id": 1,
      "title": "Awesome Post Tile",
      "content": "Post content"
    },
    {
      "id": 2,
      "title": "Another Post Tile",
      "content": "Another post content"
    }
  ]
}
```

Version data entries

12 entries across 12 versions & 2 rubygems

Version Path
active_model_serializers-0.10.0.rc3 docs/howto/add_root_key.md
cheap_ams-0.10.11 docs/howto/add_root_key.md
cheap_ams-0.10.10 docs/howto/add_root_key.md
cheap_ams-0.10.8 docs/howto/add_root_key.md
cheap_ams-0.10.7 docs/howto/add_root_key.md
cheap_ams-0.10.6 docs/howto/add_root_key.md
cheap_ams-0.10.5 docs/howto/add_root_key.md
cheap_ams-0.10.4 docs/howto/add_root_key.md
cheap_ams-0.10.3 docs/howto/add_root_key.md
cheap_ams-0.10.2 docs/howto/add_root_key.md
cheap_ams-0.10.1 docs/howto/add_root_key.md
cheap_ams-0.10.0.rc2 docs/howto/add_root_key.md