Building REST APIs with Rails: Basic Serialization
Third video in a pile of LiveCoding.tv videos, shows how to use ActiveModel Serializer to shape the output of your resources. I totally forgot how links work, so watch me flap around trying to get it working and chuckle as I flail.
Notes
To get going, you’ll need to include in your Gemfile
:
gem 'active_model_serializers', '~> 0.10.0'
Install that by running bundle:
$ bundle install
Now there will be a new generator available, called resource
:
$ rails g resource manufacturer name:string about:string city:string country:string manufacturer:references
$ rails g resource product name:string description:string product_type:string apv:float image_url:string references:products
These fields are taken from our API Blueprint documentation, and we already know the types.
rake db:create db:migrate
Create and generate the tables from our migrations, turning them into actual schema we can populate using the rails console:
$ rails console
manufacturer = Manufacturer.create(name: "Thatchers", about: "Pretty solid cider makers who are randomly moving their factories in the south west and going to Ireland...", city: "Dublin", country: "Ireland")
Product.create(manufacturer: manufacturer, name: 'Katies', description: "Unnecessarily strong fizzy cider that sells for the same price as normal ciders.", apv: 7.6, product_type: 'cider')
Product.create(manufacturer: manufacturer, name: 'Thatchers Dry', description: "As the name suggests this is dry, and a little tangy.", apv: 6.5, product_type: 'cider')
Go look at ./app/controllers
and ./app/serializers
for the rest… more notes later.
Don’t forget to make ./config/initializers/serializers.rb
and fill it with:
ActiveModelSerializers.config.adapter = :json_api
Then shove this in ./config/application.rb
:
config.after_initialize do
Rails.application.routes.default_url_options[:host] = ENV['HOST'] || 'localhost:3000'
end
By the time all that’s done, you should have some slick JSON-API output:
{
"data":{
"id":"1",
"type":"manufacturer",
"attributes":{
"name":"Thatchers",
"about":"Pretty solid cider makers that are ditching their factories in the south west and going to Ireland...",
"city":"Dublin",
"country":"Ireland"
},
"relationships":{
"products":{
"data":[
{
"id":"1",
"type":"product"
}
]
}
},
"links":{
"self":"http://localhost:3000/manufacturers/1",
"products":{
"href":"/products?manufacturer=1"
}
}
}
}
Cheers.
Interested in a Proper Series?
These rough videos will be replaced with a nicely recorded series, and to get updates about that, just let me know your email address.