ActiveModel::Serializers - JSON API Heaven
I've used Rabl & Jbuildler (my favorite so far) as well as overriding good old to_json
on my models to craft JSON responses for API's for quite awhile now. I thought that Jbuilder might be the best that it got, with a nice DSL for use in the view layer, and partials.
Today, I want to introduce you to Active Model Serializers. Essentially follows the decorator pattern for transforming your model instances into JSON.
Very clean, and the best part? I also use Inherited Resources and this integrates into all my InheritedResources responses without any effort or having to define view files.
Here's a sample of all the pieces playing well together: https://gist.github.com/4280164
Written by Matt Brewer
Related protips
4 Responses
FYI your Gist link is busted
broken gist link
Found his Github Gists; nothing relating to ActiveModel::Serializers
. Must have been taken down for some reason, Matt?
Must have removed it at some point, sorry everyone!