When to use active model serializer ?
When you have your app running on both browser and mobile app, and if you have a backend rails server then active model serialiser is a perfect gem for serving APIs.
There are other gems like rabl but I personally prefer
active_model_serializers gem because this is extremely easy to use and it has lot of + points.
Step 1 : Add it to your Gemfile
Step 2 : Create a serializer
Create a serializer file by running
which will create
Step 3 :
In serializer, you can add attributes, associations, custom methods.
1 2 3
So here id and name are user fields. Followers is an association in user. timestamp is a custom method.
You can define that method like this.
1 2 3
Step 4 :
In your controller, make sure you are rendering json and html
When you visit
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17