mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 06:16:50 +00:00
Use 1.9 hashes in the readme
This commit is contained in:
parent
4f185efa90
commit
88ff42ebc8
14
README.md
14
README.md
@ -80,7 +80,7 @@ for a serializer for the object and use it if available.
|
|||||||
class PostsController < ApplicationController
|
class PostsController < ApplicationController
|
||||||
def show
|
def show
|
||||||
@post = Post.find(params[:id])
|
@post = Post.find(params[:id])
|
||||||
render :json => @post
|
render json: @post
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
```
|
```
|
||||||
@ -107,7 +107,7 @@ 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
|
||||||
@ -124,7 +124,7 @@ end
|
|||||||
class PostsController < ApplicationController
|
class PostsController < ApplicationController
|
||||||
def index
|
def index
|
||||||
@posts = Post.all
|
@posts = Post.all
|
||||||
render :json => @posts
|
render json: @posts
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
```
|
```
|
||||||
@ -145,7 +145,7 @@ By default, the root element is the name of the controller. For example, `PostsC
|
|||||||
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
|
||||||
@ -162,7 +162,7 @@ root element of the array with any of those methods 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
|
||||||
```
|
```
|
||||||
|
|
||||||
## Disabling the root element
|
## Disabling the root element
|
||||||
@ -186,7 +186,7 @@ end
|
|||||||
#### 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. Subclass the serializer, and specify using it
|
#### 3. Subclass the serializer, and specify using it
|
||||||
@ -197,7 +197,7 @@ class CustomArraySerializer < ActiveModel::ArraySerializer
|
|||||||
end
|
end
|
||||||
|
|
||||||
# controller:
|
# controller:
|
||||||
render :json => @posts, :serializer => CustomArraySerializer
|
render json: @posts, serializer: CustomArraySerializer
|
||||||
```
|
```
|
||||||
|
|
||||||
#### 4. Define default_serializer_options in your controller
|
#### 4. Define default_serializer_options in your controller
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user