mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 07:16:49 +00:00
Add docs for the fields option
This commit is contained in:
committed by
Yohan Robert
parent
bcf3358524
commit
d27b21a733
31
docs/general/fields.md
Normal file
31
docs/general/fields.md
Normal file
@@ -0,0 +1,31 @@
|
||||
[Back to Guides](../README.md)
|
||||
|
||||
# Fields
|
||||
|
||||
If for any reason, you need to restrict the fields returned, you should use `fields` option.
|
||||
|
||||
For example, if you have a serializer like this
|
||||
|
||||
```ruby
|
||||
class UserSerializer < ActiveModel::Serializer
|
||||
attributes :access_token, :first_name, :last_name
|
||||
end
|
||||
```
|
||||
|
||||
and in a specific controller, you want to return `access_token` only, `fields` will help you:
|
||||
|
||||
```ruby
|
||||
class AnonymousController < ApplicationController
|
||||
def create
|
||||
render json: User.create(activation_state: 'anonymous'), fields: [:access_token], status: 201
|
||||
end
|
||||
end
|
||||
```
|
||||
|
||||
Note that this is only valid for the `json` and `attributes` adapter. For the `json_api` adapter, you would use
|
||||
|
||||
```ruby
|
||||
render json: @user, fields: { users: [:access_token] }
|
||||
```
|
||||
|
||||
Where `users` is the JSONAPI type.
|
||||
@@ -73,7 +73,12 @@ See [ARCHITECTURE](../ARCHITECTURE.md) for more information.
|
||||
|
||||
#### fields
|
||||
|
||||
PR please :)
|
||||
If you are using `json` or `attributes` adapter
|
||||
```ruby
|
||||
render json: @user, fields: [:access_token]
|
||||
```
|
||||
|
||||
See [Fields](fields.md) for more information.
|
||||
|
||||
#### adapter
|
||||
|
||||
@@ -83,7 +88,7 @@ PR please :)
|
||||
|
||||
```render json: posts, each_serializer: PostSerializer, key_transform: :camel_lower```
|
||||
|
||||
See [Key Transforms](key_transforms.md) for more informaiton.
|
||||
See [Key Transforms](key_transforms.md) for more information.
|
||||
|
||||
#### meta
|
||||
|
||||
|
||||
Reference in New Issue
Block a user