mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-24 14:56:50 +00:00
cleanup markdown in docs
This commit is contained in:
parent
53da0b12fd
commit
f8263153b6
@ -72,20 +72,20 @@ serializer and available as `@options` inside.
|
|||||||
|
|
||||||
To specify a custom serializer for an object, there are 2 options:
|
To specify a custom serializer for an object, there are 2 options:
|
||||||
|
|
||||||
1. Specify the serializer in your model:
|
#### 1. Specify the serializer in your model:
|
||||||
|
|
||||||
```ruby
|
```ruby
|
||||||
class Post < ActiveRecord::Base
|
class Post < ActiveRecord::Base
|
||||||
def active_model_serializer
|
def active_model_serializer
|
||||||
FancyPostSerializer
|
FancyPostSerializer
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
end
|
||||||
```
|
```
|
||||||
|
|
||||||
2. Specify the serializer when you render the object:
|
#### 2. Specify the serializer when you render the object:
|
||||||
|
|
||||||
```ruby
|
```ruby
|
||||||
render :json => @post, :serializer => FancyPostSerializer
|
render :json => @post, :serializer => FancyPostSerializer
|
||||||
```
|
```
|
||||||
|
|
||||||
## Arrays
|
## Arrays
|
||||||
@ -123,33 +123,33 @@ By default, the root element is the name of the controller. For example, PostsCo
|
|||||||
generates a root element "posts". To change it:
|
generates a root element "posts". To change it:
|
||||||
|
|
||||||
```ruby
|
```ruby
|
||||||
render :json => @posts, :root => "some_posts"
|
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 3 options:
|
more concise json. To disable the root element for arrays, you have 3 options:
|
||||||
|
|
||||||
1. Disable root globally for in ArraySerializer. In an initializer:
|
#### 1. Disable root globally for in ArraySerializer. In an initializer:
|
||||||
|
|
||||||
```ruby
|
```ruby
|
||||||
ActiveModel::ArraySerializer.root = false
|
ActiveModel::ArraySerializer.root = false
|
||||||
```
|
```
|
||||||
|
|
||||||
2. Disable root per render call in your controller:
|
#### 2. Disable root per render call in your controller:
|
||||||
|
|
||||||
```ruby
|
```ruby
|
||||||
render :json => @posts, :root => false
|
render :json => @posts, :root => false
|
||||||
```
|
```
|
||||||
|
|
||||||
3. Create a custom ArraySerializer and render arrays with it:
|
#### 3. Create a custom ArraySerializer and render arrays with it:
|
||||||
|
|
||||||
```ruby
|
```ruby
|
||||||
class CustomArraySerializer < ActiveModel::ArraySerializer
|
class CustomArraySerializer < ActiveModel::ArraySerializer
|
||||||
self.root = "items"
|
self.root = "items"
|
||||||
end
|
end
|
||||||
|
|
||||||
# controller:
|
# controller:
|
||||||
render :json => @posts, :serializer => CustomArraySerializer
|
render :json => @posts, :serializer => CustomArraySerializer
|
||||||
```
|
```
|
||||||
|
|
||||||
Disabling the root element of the array with any of the above 3 methods
|
Disabling the root element of the array with any of the above 3 methods
|
||||||
@ -165,7 +165,7 @@ will produce
|
|||||||
To specify a custom serializer for the items within an array:
|
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
|
||||||
```
|
```
|
||||||
|
|
||||||
## Getting the old version
|
## Getting the old version
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user