Skip to content

Instantly share code, notes, and snippets.

@blaisethomas
Created August 8, 2016 05:49
Show Gist options
  • Save blaisethomas/ee1c2259c6992549d4de11df77d3bfbb to your computer and use it in GitHub Desktop.
Save blaisethomas/ee1c2259c6992549d4de11df77d3bfbb to your computer and use it in GitHub Desktop.
API in Rails

API in Rails

(Cheatsheet with no coding or local installations needed in approximately 15 minutes)

##Intro

Here are some instructions to build and deploy a RESTful JSON API. Backed by Rails and a SQL database (postgresql), deployed to heroku, and publically accessible. There are 3 quick & easy steps:

  1. use prelang.com to scaffold a rails app on your github account, based on a data model (entity relationship model) of your choice
  2. use cloud9.io to access the code and prepare it to be deployed to Heroku.
  3. Heroku: a platform as a service (PaaS) that enables developers to build, run, and operate applications entirely in the cloud.

Prelang is an automated rails building tool

  1. Go to prelang.com and Sign-in w/GitHub
  2. Create New Project
  3. Name
  4. Select DB: PostgreSQL
    (PostgreSQL REQUIRED, 5432)
  5. Add Feature
    ◊ Add Scaffolding (Future "Ambitious" use P3: Demo Video; RAILS. Add 'User Login', without code. Uses GUI in "Models")
    NOTE: 'Scaffolding' is fine for practice/learning, bad practice when charging users.
  6. Add Model
    'Post'
    ◊ String: title (100s char limit)
    ◊ Text: description (166 char limit)
    ◊ URL: link
    (Future "Ambitious" use: Add Comment; Text: commentBody, BelongsTo: Post > Post>>HasMany: Comment)
    ◊ Scaffold: ON
  7. Generate Project
    Auto-pushes to GitHub (URL) and creates a repo for you.

Cloud 9 is a hosted development environment with all the tools you need to work on a Rails project, so that you need not install Ruby/Rails on your local machine.

  1. Go to Cloud 9, log in.
  2. Create a new workspace
  3. Workspace name:
    ◊ Name: ________
    ◊ Description: ________
    ◊ Clone from Git (Use the github url that prelang generated.)
  4. Template: Rails
    ◊ Create workspace (You should now see your Rails project code.)
  5. Navigate to app/controllers
    ◊ application_controller.rb
    ◊ Change~ :exception > :null_session (Default: Ln4)
    ◊ Ctrl + S (There is no Auto-save)
  6. Open TERMINAL:
    $ git add -A
    $ git commit -m "Makes: available with null_session"
    $ git push origin master
    AUTH: User Id & Pass (Unless SSH configured)

###3. heroku.com

Heroku will host our newly created Rails app live on the web so that we can interact with it. It will also give us a PostgreSQL database.

  1. Go to heroku and log in
  2. Create a new app in the heroku GUI.
    Make a note of the app name (referred to later as instancename)
  3. TERMINAL (in Cloud 9)
    ◊ heroku login
    AUTH: User Id & Pass
    $ heroku git: remote -a instancename
    $ git remote -v (recheck git)
    $ git push heroku master
    *Installs Gems… Ignore 'WARNING'*s (Ruby being fussy)
    $ git remote -v (you should now see a new remote for heroku)
    $ bundle install
    $ heroku run rake db:migrate
    Like gradle for Rails; executes SQL commands

That's it! You should have a working API deployed to the web.

You can now navigate to: instancename.heroku.com/modelname

Or just use this as an API

In addition you will be able to interact with this DB through your web browser:

  1. BROWSER:
    instancename.heroku.com/modelname (plural)
  2. Editing Post
    ◊ Title
    ◊ Description
    ◊ Link
  3. Create Post
    instancename.heroku.com/posts.json
    Displays full RESTful json.
  4. TERMINAL:
    $ rake routes
  5. POSTMAN
    ◊ POST instancename.herokuapp.com/posts.json
    ◊ Body
    ◊ raw
    ◊ JSON (application/json)
    { "title" : "test"
    "description : " testing",
    "link" : "insert_valid_link_post"
    }
  6. GET instancename.herokuapp.com/posts/2.json
    Returns json

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment