2013-12-06



This article is a follow-up to Nitrous.IO: Rails Development in the Cloud published earlier. If you have not read that article, I recommend you do so. I will wait.

Although it is not a prerequisite, this tutorial assumes you have an account on Nitrous.IO and have setup a Box for Ruby.

Start with a Nitrous Box

If you have not created a Box yet on Nitrous.io, select the Boxes menu and create a new Box.

The Nitrous Box must be connected to your Github account. These instructions in the Nitrous Help Center are the best guide to accomplish that task.

Login to Heroku

This assumes you already have an account with Heroku. If not create one now.

Heroku provides a very detailed API (called Toolbelt) and it is part of the Nitrous instance when you create a Box.

You are expected to provide your login email and password. This should be greeted with an acknowledgement:

If this is the first time using this Nitrous Box to deploy to Heroku, you need to add your SSH key to Heroku. If you are not sure whether this step has been accomplished in the past, it will not hurt to just run the command again.

Create Your App

Create a Rails Application

Next you can simply create a Rails application. We will specify a PostgreSQL database (because that is what all the ‘cool kids’ are using nowadays).

Setup Git

The usual (and should be quite familiar) initial commit in Git to get it setup goes like this:

Create a New Application on Heroku

With the Heroku Toolbelt you create an app on Heroku with this simple command:

It will respond with the name of the app on Heroku. I recommend you copy the peculiar (and often poetic) application name for use in a later step.

Since Bundler is also included in the Nitrous instance, add these lines in the Gemfile:

You should use RVM to install Ruby 2.0.0 on Nitrous. RVM is also included in every Nitrous box, so this is as simple as rvm install 2.0.0 followed by rvm use 2.0.0

Setting up a PostgreSQL database on Heroku is next.

Using the Heroku Toolbelt,

will add a PostgreSQL database (the free level) and return a HEROKU_POSTGRES_COLOR_URL. Take note of the actual COLOR that is returned.

Then

to establish the database (You need to log into your Heroku Dashboard to get the PostreSQL details).

Normally, when creating a Rails application, the next step would be to edit the config/database.yml file. But with Heroku, your production database details are applied automatically on the Heroku server regardless of what you place in the database.yml file.

To use a Heroku PostgreSQL database for development also, follow the instructions here. If you prefer a local SQLite database for development, those instructions are at the end of this article.

Now it is time for Bundler to work its magic for us and pull all the pieces together.

Setup the Asset Pipeline

Add a line in your config/application.rb

Commit in Git

Deploy the Application to Heroku

Deploying to Heroku is the simplest part of this whole process. I truly appreciate that because it is the part that I do most often in a continuous development cycle.

Setting Up the Server on Heroku

If you want to remain within the FREE realm, use a single dyno on Heroku.

Visit the App on the Server

At this point I like to perform a quick “smoke test” just to be sure we didn’t miss any of the essential steps. This saves a lot of headaches later when you are troubleshooting the application because you can be assured it works at the base level.
This is where you will use the [sometimes poetic, sometimes profound] name Heroku created for your application. You can paste the name from your clipboard (if you followed my earlier advice)

Or you can navigate to the instance (based on the name that it returned earlier):

I just open another tab in my browser that is currently connected to Nitrous.

You should get back the Rails default index.html page. We have not yet created any models, controllers, or routes so the “About your application’s environment” link will yield a server error.

That is to be expected.

To view the application logs, with Heroku ToolBelt, use heroku logs. The ‘status=200′ on the very last line is what we expect to see.

Now we need to migrate the database.

A Quick Assesment of Our Progress

You have successfully created and deployed a Rails application ENTIRELY FROM WITHIN A BROWSER !

Leveraging Nitrous.IO as a cloud-based development platform and Heroku as a cloud-based SAAS host, you can be completely divorced and indiscriminate about your client hardware and/or software.

Set Up the Development Server

In order to transform this empty, benign Rails application into your next big money-making Disruptive Web Product, we need to do some heavy testing-coding-refactoring Agile development.

Let’s now get our Development environment setup.

In the Gemfile add gem 'unicorn', group: :development and run bundle install to install it locally.

To test (and for each iteration during development) run this command

Use the Nitrous menu Preview -> Port 3000 to test your development environment

One Final Commit

If you prefer unicorn for production, here are details on setting it up.

Final Cleanup

Of course, it is important to rm the public/index.html file.

I recommend you also maintain the project in your Github account. As a public repository you can get help from the Open Source community in the form of contributions and Pull Requests. With a Private repository, you can collaborate with other developers on your team and maintain the central source control.

The Nitrous Box must be connected to your Github account. This great article in the Nitrous Help Center explains how to setup the Github Private Key.

First create the repository on Github. Then issue these two commands:

Next, write some tests.

And then you will create some models, controllers and views and begin actually developing the application.

Run the unicorn server with this command

Use the Nitrous menu Preview -> Port 3000 to test your development environment

Update the Development Environment

If you prefer to use sqlite in development (which I do) then add these steps.

Modify the config/database.yml for ‘development’ (and ‘test’) like this:

Add sqlite3 to the Gemfile

Call on Bundler

Then Migrate the database

Give it a test drive by calling the unicorn server

And then use the Nitrous menu Preview -> Port 3000 to open it in a browser

You now have complete development and production environments, along with git-based deployment, and you didn’t need to install anything on your local computer.

I am excited to usher-in a new Browser-based workflow.

The post Nitrous.IO and Heroku: A Perfect Pair appeared first on SitePoint.

Show more