RAILS API ON AWS – PART 2: Basic Rails App with PostGIS

In part 2, we create basic Rails API-only application backed by PostgreSQL database with PostGIS extension.

 

List of Posts:

  1. part 1: Local server setup
  2. part 2: Basic Rails App with PostGIS
  3. part 3: Docker
  4. part 4: AWS setup
  5. part 5: AWS setup & deploy

 

 

  1. Introduction
  2. Create Rails App
  3. First simple action
  4. Places – model & controller
  5. Git

1. Introduction

In part 1, I installed: Ruby, Rails, Docker, NGINX, PostgreSQL with PostGIS and setup Git. You can find there also a cheat sheet of basic commands for RoR, Docker and PostgreSQL.

In this episode, we create a basic Rails application allowing to add Place with geographical coordinates to PostGIS database and get all Places.

Part 3 shows how to dockerize Rails application.

 

2. Create Rails App

My folder structure will be different from a typical approach:

 

 

Create App

Go to the home directory and create a new folder:

Create new API-only application with PostgreSQL database:

 

Setup for PostGIS*

*Skip this step, if you don’t use PostGIS.

Add to Gemfile:

Change adapter in app/config/database.yml:

 

Setup Database

Add to app/config/database.yml:

Now set RAOW_DB_USER and RAOW_DB_PASS as environment variables.Open ~/.bashrc:

Add variables at the end of the file and replace username/password with PostgresSQL username/password (I’ve created a user in part 1):

Create database:

 

Run Rails app

Skip adding -b or replace xxx.xxx.xx.xx with IP of your’s server.

Open in web browser: xxx.xxx.xx.xx:3003. You should see Rails Welcome screen.

 

3. First simple action

Let’s create first very simple action:

app/app/controllers/status_check_controller.rb:

Run Rails server and open in a web browser:

You should see:

 

4. Places – model & controller

Our simple app will be used to manage locations with geographical coordinates. I call them Places.

Model

Create model:

Edit migration file in app/db/migrate/xxx_create_places.rb:

Controller

I implement only 2 actions:

  • index – to list all points
  • create – to add new points

Add resources to app/config/routes.rb:

Generate controller:

Edit app/app/controllers/places_controller.rb:

And edit Place model (app/app/models/place.rb):

 

Test

Run Rails server:

Using Postman we can test both actions.

Create

places_create

 

Get all places

get place

 

 

 

5. Git

You can find the source code for this tutorial on here on Github.

You can create your own repository on Github / Bitbucket / Gitlab. In my project structure, Rails application is in a subfolder: app, and I initialize repo in main folder: rails_api_on_aws:

 

Initialize repo in rails_api_on_aws directory:

Add .gitignore:

Remove git from Rails app folder:

Back to the main folder:

Add files, commit and push:

 

Here you can find version of repo for part 2 of this tutorial:

https://github.com/tomaszantas/rails-api-on-aws/releases/tag/part2

 

Tomasz Antas

Ruby on Rails developer and Web designer.
The area of his interest includes Popular Science, Internet of Things, Wearables, AI and Virtual/Augmented Reality.

Latest posts by Tomasz Antas (see all)

Thanks for reading!