Using JSON API to join a Rails backend to an Angular app Code for a practical example

Introduction

I prefer to keep the UI code separate from the Ruby on Rails server. The advantages are that the complexity of using Ruby on Rails to serve up assets disappears and also that development work on the front end can be done without knowing anything about ruby.  More than one app can use the same API, perhaps an Ionic app for mobile and an Angular app for desktop admin.

Its possible to send raw JSON backwards and forwards, but we quickly get into decisions about pagination, sorting and filtering, and the API itself can become a bit inconsistent and expensive to own and maintain.

JSON-API solves this problem, but you need implementations of this standard in both ruby and javascript.  This article gives example code that uses JSON-API to unify the two.

The finished app

To ease the suspense, here are some screenshots of the finished app.   It is part of a larger project to help people build templates for checklists that will be completed using another mobile app.

The UI is Angular 2 and Material design.  The server is a Rails app that uses an engine containing the API.  All the code is available.

List of templates, clicking on one takes you to the form shown next

A checklist on the right is being built from components on the left.

The screenshot shows a signature component being dragged into a checklist template called ‘Inspection of a rented house …’.

The template will be used by an Ionic mobile app to collect the information, photos, descriptions and signatures from inspections in the real world.  The Ionic app can use the same Rails API.

The API

A Rails engine provides three resources, audit_type, audit_type_components and available_component_types. The app does not have any authentication because its Rails Engine and Angular components are meant to be used in larger projects.

The routes file looks like this.

and the routes are

 

Using JSON API on both sides of the API

JSON API is complicated internally, and very pedantic about the structure of data and relationships.  So its a bad idea to assemble or parse the json with your own code.

There is a great gem called JSONAPI::Resources to simplify the server code,  and a corresponding adaptor for Angular called Angular2 JSON API.   Together they provide slick way to move representations of objects from ruby to javascript and back again, without having to delve into the structure of the JSON itself.

Server Side:  JSON API Resources

This lovely gem lets you define resources for use by the API.  Instead of writing controllers to handle the REST actions, we leave it up to the gem.

The routes.rb file, shown above, defines resources with the jsonapi_resources  method which orchestrates which controller to call.

The controllers are greatly simplified because they just need to inherit from JSONAPI::ResourceController.

JSONAPI::Resources::Matchers is a neat gem that provides spec matchers to test the API,  although at the time of writing, the tests just check the response status and any objects returned in the data.

JSON API Resources takes a huge amount of complexity out of the hands of the developer, but the error messages can be so vague that you may have to dive into the gem’s code to figure out what is wrong with the call.  For instance I ran into a mix up with the use of underscores in the names of routes and keys.  The fix was to explicitly set underscores in the initializer.

 

Client Side: Angular 2 JSON API

Again, this component lets you abstract the models used by the JSON API.  First create a  a Datastore service by extending JsonApiDatastore

 

 

Then define models corresponding to the resources on the server side

 

In the Angular view controller, you can call the API and wait for the results

 

In both the ruby and Typescript code,  we’re spared from parsing or building the complicated json relationships and lists of associated objects.

On several occasions I needed to look into the Angular 2 JSON API code to figure out why something was not working for me.  It was always because I was calling the server with the wrong route name, but this was time consuming to understand.

For the drag and drop I used ng2-dragular, which was fun to use.

Development tools

Another advantage of clearly separating the server and client code is that you can use the right development tool for the job.

I use Redmine to develop the Rails project and WebStorm for the Angular work.    Both of these JetBrains IDEs are really similar to use, but specialised for the languages used.

Angular view code can become verbose, but Angular 2 has a neat way of defining components that can nested.

Each view component is organised into a collection of small files

Material Design proved to be a pain when laying out admin pages.  For Desktop apps I think that Bootstrap is easier to use.

The code is on Github

There are three repositories for this project.

  1. A rails engine to encapsulates the models and controllers
  2. Material Design / Angular 2 app
  3. Rails app which includes both of the above

The rails app is just a shell that mounts the engine and includes the UI code in its public directory.    (Copy the contents of the dist directory of the UI into the public directory of the Rails app).

The UI assumes that Rails will be started on port 3000, this needs to be stored in an environment variable.

I put the server side models into an engine so that it can used in more than one future project.

Installing and running the demo app

The demo app uses Postgres.

 

Then navigate to localhost:3000, choose a checklist template and drag the components around.  They should be saved to the database.

Rationale for the work

I kept coming across the need for a convenient and cheap way to build checklists that describe how to do a specific task. For instance;

– for an engineer servicing a burglar alarm
– a pest control person visiting a property to remove an infestation
– an estate agent checking on the state of a rental property

Checklists are used to tell the agent where the job is,  who it is for,  what steps to carry out.  The same report can also be used to record the customer’s signature,  any materials used and time starting and stopping work.

Although each of these reports are different, they all have the same component parts

  • Title
  • Address
  • Photo
  • Text description
  • Signature
  • Yes / No box answering a question
  • Choice / Select box
  • Sections

So the admin app provides a way to drag and drop these components into an ordered checklist.

Once a template is ready, it can be used by somebody with another mobile app to carry out the checklist and save the results back to the API.  This is done by another app written in Ionic.  Authentication is by token, not described here.  Hosting is on Heroku.

A basis for other projects

Projects sometimes feel like walks through a forest, if you know what you’re doing then its great, but step off into the under growth and you’re in for a long, scratchy afternoon.

The JSONAPI:Resources gem and the Angular 2 JSON API adapter work together to hide the complexity of JSON API from the server and client code.  They provide ways to paginate, sort and filter resources.

Best of all, they free up time for the developer to spend on designing a cool user interface and talk to the users.

Guy Roberts

About Guy Roberts

A freelance developer who conjures up Software as a Service using Angular JS, SQL, Ruby on Rails and Java. Runner, dad and glider pilot.