From a93ddda83eb7a29f8d920a7684057b54a31ab9a0 Mon Sep 17 00:00:00 2001 From: Pericles Theodorou Date: Sat, 12 Sep 2015 10:50:32 +0100 Subject: [PATCH] Documentation for serializing resources without render --- README.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/README.md b/README.md index 9cba3920..73e073bb 100644 --- a/README.md +++ b/README.md @@ -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: