Skip to content

Commit b9b94f0

Browse files
committed
Merge pull request #1139 from PericlesTheo/serializer_without_render_doc
Documentation for serializing resources without render
2 parents 4399c1a + a93ddda commit b9b94f0

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,23 @@ render json: @post, meta: { total: 10 }, meta_key: "custom_meta"
138138

139139
`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`.
140140

141+
### Using a serializer without `render`
142+
143+
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
144+
the resource you want to be serialized and call `.serializable_hash`.
145+
146+
```ruby
147+
def create
148+
@message = current_user.messages.create!(message_params)
149+
MessageCreationWorker.perform(serialized_message)
150+
head 204
151+
end
152+
153+
def serialized_message
154+
ActiveModel::SerializableResource.new(@message).serializable_hash
155+
end
156+
```
157+
141158
### Overriding association methods
142159

143160
If you want to override any association, you can use:

0 commit comments

Comments
 (0)