- Use Active Model Serializer to render JSON
- Use Active Model Serializer to render JSON for associations
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 sOur 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.
Set up a serializer for Author that returns the following JSON:
- The author's
name - The author's
profile, including only theusername,email,bioandavatar_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.
Set up a serializer for the Post model that displays the following:
- The post's
titleandcontent - The name of the author
- The tags associated with the post
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