mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-24 14:56:50 +00:00
Create new section 'Disabling root' in README
This commit is contained in:
parent
358b6a8875
commit
1614ef3b12
71
README.md
71
README.md
@ -139,39 +139,8 @@ render :json => @posts, :root => "some_posts"
|
|||||||
```
|
```
|
||||||
|
|
||||||
You may disable the root element for arrays at the top level, which will result in
|
You may disable the root element for arrays at the top level, which will result in
|
||||||
more concise json. To disable the root element for arrays, you have 4 options:
|
more concise json. See the next section for ways on how to do this. Disabling the
|
||||||
|
root element of the array with any of those methods will produce
|
||||||
#### 1. Disable root globally for `ArraySerializer`. In an initializer:
|
|
||||||
|
|
||||||
```ruby
|
|
||||||
ActiveSupport.on_load(:active_model_serializers) do
|
|
||||||
# Disabled for all serializers
|
|
||||||
# ActiveModel::Serializer.root = false
|
|
||||||
|
|
||||||
# Or just for the ArraySerializer
|
|
||||||
ActiveModel::ArraySerializer.root = false
|
|
||||||
end
|
|
||||||
```
|
|
||||||
|
|
||||||
#### 2. Disable root per render call in your controller:
|
|
||||||
|
|
||||||
```ruby
|
|
||||||
render :json => @posts, :root => false
|
|
||||||
```
|
|
||||||
|
|
||||||
#### 3. Create a custom `ArraySerializer` and render arrays with it:
|
|
||||||
|
|
||||||
```ruby
|
|
||||||
class CustomArraySerializer < ActiveModel::ArraySerializer
|
|
||||||
self.root = false
|
|
||||||
end
|
|
||||||
|
|
||||||
# controller:
|
|
||||||
render :json => @posts, :serializer => CustomArraySerializer
|
|
||||||
```
|
|
||||||
|
|
||||||
Disabling the root element of the array with any of the above 3 methods
|
|
||||||
will produce
|
|
||||||
|
|
||||||
```json
|
```json
|
||||||
[
|
[
|
||||||
@ -185,6 +154,42 @@ To specify a custom serializer for the items within an array:
|
|||||||
```ruby
|
```ruby
|
||||||
render :json => @posts, :each_serializer => FancyPostSerializer
|
render :json => @posts, :each_serializer => FancyPostSerializer
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Disabling the root element
|
||||||
|
|
||||||
|
You have 4 options to disable the root element, each with a slightly different scope:
|
||||||
|
|
||||||
|
#### 1. Disable root globally for all, or per class
|
||||||
|
|
||||||
|
In an initializer:
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
ActiveSupport.on_load(:active_model_serializers) do
|
||||||
|
# Disabled for all serializers
|
||||||
|
ActiveModel::Serializer.root = false
|
||||||
|
|
||||||
|
# Or just for the ArraySerializer
|
||||||
|
# ActiveModel::ArraySerializer.root = false
|
||||||
|
end
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 2. Disable root per render call in your controller
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
render :json => @posts, :root => false
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 3. Subclass the serializer, and specify using it
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
class CustomArraySerializer < ActiveModel::ArraySerializer
|
||||||
|
self.root = false
|
||||||
|
end
|
||||||
|
|
||||||
|
# controller:
|
||||||
|
render :json => @posts, :serializer => CustomArraySerializer
|
||||||
|
```
|
||||||
|
|
||||||
#### 4. Define default_serializer_options in your controller
|
#### 4. Define default_serializer_options in your controller
|
||||||
|
|
||||||
If you define `default_serializer_options` method in your controller,
|
If you define `default_serializer_options` method in your controller,
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user