Merge pull request #1139 from PericlesTheo/serializer_without_render_doc

Documentation for serializing resources without render
This commit is contained in:
L. Preston Sego III 2015-09-12 10:46:55 -04:00
commit b9b94f00ff

View File

@ -138,6 +138,23 @@ render json: @post, meta: { total: 10 }, meta_key: "custom_meta"
`meta` will only be included in your response if you are using an Adapter that supports `root`, as JsonAPI and Json adapters, the default adapter (FlattenJson) doesn't have `root`.
### Using a serializer without `render`
At times, you might want to use a serializer without rendering it to the view. For those cases, you can create an instance of `ActiveModel::SerializableResource` with
the resource you want to be serialized and call `.serializable_hash`.
```ruby
def create
@message = current_user.messages.create!(message_params)
MessageCreationWorker.perform(serialized_message)
head 204
end
def serialized_message
ActiveModel::SerializableResource.new(@message).serializable_hash
end
```
### Overriding association methods
If you want to override any association, you can use: