Serializers are presenters for your API
Ruby on Rails is popular for both HTML-first and API-first applications. So far in this chapter, we’ve only considered HTML-driven applications, with templates, view helpers, and so on—all the frontend things. What about pure backend Rails applications? Is there a representation layer? Sure, there is.
Although API-only Rails applications delegate most of the browser-related work to frontend frameworks, it’s still the responsibility of the application’s presentation layer to prepare responses to HTTP requests.
In most cases, we deal with JSON responses. So, let’s discuss how we can turn our application’s data into JSON strings and how the presenter pattern could be used here, too.
From model to JSON
In Rails, there are a couple of built-in options for generating JSON responses. Let’s first discuss them.
Using #as_json
Rails makes it dead simple to respond with JSON from controllers...