mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 06:16:50 +00:00
updating docs to remove comments about generators and clarifying default to_json functionality
This commit is contained in:
parent
2f6d1e26ed
commit
2b2f3e62e8
@ -17,9 +17,6 @@ For now, the easiest way to install `ActiveModel::Serializers` is to add this to
|
|||||||
Then, install it on the command line:
|
Then, install it on the command line:
|
||||||
|
|
||||||
$ bundle install
|
$ bundle install
|
||||||
$ rails g serializers:install
|
|
||||||
|
|
||||||
The installation will generate an `app/serializers` directory and create an `ApplicationSerializer` inside it. You can use the `ApplicationSerializer` for common functionality, and to customize the behavior of embedded associations. See more on this later.
|
|
||||||
|
|
||||||
# Creating a Serializer
|
# Creating a Serializer
|
||||||
|
|
||||||
@ -31,6 +28,10 @@ This will generate a serializer in `app/serializers/post_serializer.rb` for your
|
|||||||
|
|
||||||
$ rails g serializer post
|
$ rails g serializer post
|
||||||
|
|
||||||
|
# ApplicationSerializer the global serializer
|
||||||
|
|
||||||
|
All new serializers descend from either ActiveModel::Serializer or from ApplicationSerializer if you create this file in `app/serializers`. This file is no longer required.
|
||||||
|
|
||||||
# render :json
|
# render :json
|
||||||
|
|
||||||
In your controllers, when you use `render :json`, Rails will now first search for a serializer for the object and use it if available.
|
In your controllers, when you use `render :json`, Rails will now first search for a serializer for the object and use it if available.
|
||||||
@ -42,10 +43,14 @@ In your controllers, when you use `render :json`, Rails will now first search fo
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
In this case, Rails will look for a serializer named `PostSerializer`, and if it exists, use it to serialize the `Post`. If it does not exist, Rails will simply call `to_json` on the `@post` object.
|
In this case, Rails will look for a serializer named `PostSerializer`, and if it exists, use it to serialize the `Post`.
|
||||||
|
|
||||||
This also works with `render_with`, which uses `to_json` under the hood. Also note that any options passed to `render :json` will be passed to your serializer and available as `@options` inside.
|
This also works with `render_with`, which uses `to_json` under the hood. Also note that any options passed to `render :json` will be passed to your serializer and available as `@options` inside.
|
||||||
|
|
||||||
|
## Getting the old version
|
||||||
|
|
||||||
|
If you find that your project is already relying on the old rails to_json change `render :json` to `render :text => @your_object.to_json`.
|
||||||
|
|
||||||
# Attributes and Associations
|
# Attributes and Associations
|
||||||
|
|
||||||
Once you have a serializer, you can specify which attributes and associations you would like to include in the serialized form.
|
Once you have a serializer, you can specify which attributes and associations you would like to include in the serialized form.
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user