Active Model Serializer Lab
Learning Goals
- Use Active Model Serializer to render JSON
- Use Active Model Serializer to render JSON for associations
Introduction
For this lab, we're going to return to our Blog application and update it to use
ActiveModel::Serializer
for JSON serialization.
To set up the app, run:
$ bundle install
$ rails db:migrate db:seed
$ rails s
Our app includes four resources: Author
, Profile
, Post
, and Tag
, as well
as a post_tag
join table. All the associations have been set up, as well as
index
and show
routes and actions for each of the four resources.
Start by building the serializers based on the instructions below. Check your
work in the browser as you go. Then, run learn test
to make sure your code
passes the tests.
Instructions
Author
Set up a serializer for Author
that returns the following JSON:
- The author's
name
- The author's
profile
, including only theusername
,email
,bio
andavatar_url
- A list of the author's posts, including:
- the
title
- the first 40 characters of the post's content as
short_content
, with a trailing ellipsis (...
) at the end - a list of the associated
tags
Note: You will need to make serializers for the Profile
and Post
models
to get this working.
Posts
Set up a serializer for the Post
model that displays the following:
- The post's
title
andcontent
- The name of the author
- The tags associated with the post
Tags
Set up a serializer for Tag
that displays the following:
- The name of the tag
- A list of the posts the tag is associated with