Skip to content

Instantly share code, notes, and snippets.

@benstr
Last active October 16, 2016 01:26
Show Gist options
  • Save benstr/6818846 to your computer and use it in GitHub Desktop.
Save benstr/6818846 to your computer and use it in GitHub Desktop.
TUT - Start a Rails App on Nitrous.io

Deploy Development Enviroment

Be sure to replace myapp with your unique app name.

1. Start a new app on Nitrous.io

In Nitrous counsole cd into the folder you want your app to be created in. And run the following command:

rails new myapp --database=postgresql

It will take a minute but when it is done run bundle install to get all the needed extras added to your app.

HINT: windows users will need to press Shift + Ctrl + v to paste & and Shift + Ctrl + c to copy into Nitrous console.

2. Get a database on Heroku Postgres

Go login to Heroku or make a new account so you can fire up a new (and free) Postgres database. Once you are on you dashboard click Create Database and select the free dev option.

It will take a minute for the database to be provisioned. Once it is created click the name to view the connection details.

3. Connect your app to your db

In Nitrous on the file tree press the Show Hidden button and open .bashrc. Paste in the following code: this is a root file so you may have multiple HerokuPG details in this file

### Heroku Postgres Details for MyApp
  export HOST_MYAPP=
  export DB_MYAPP=
  export USER_MYAPP=
  export PORT_MYAPP=
  export PASS_MYAPP=

In HerokuPG grab the connection details and add them to the export variables in Nitrous. A final .bashrc addition should look like this:

# Heroku Postgres Details for MyApp
  export HOST_MYAPP=ec2-54-225-127-246.compute-1.amazonaws.com
  export DB_MYAPP=d183r****iu8ir
  export USER_MYAPP=fepyg****kqnfx
  export PORT_MYAPP=5432
  export PASS_MYAPP=5qdG9****4OJr225sVY4eU

In the Nitrous console run source ~/.bashrc to activate your new server variables.

Next navigate your app's database yaml file ~/myapp/config/database.ymland replace the development connection details with the following:

development:
  adapter: postgresql
  encoding: unicode
  pool: 5
  database: <%= ENV['DB_MYAPP'] %>
  username: <%= ENV['USER_MYAPP'] %>
  password: <%= ENV['PASS_MYAPP'] %>
  host: <%= ENV['HOST_MYAPP'] %>
  port: <%= ENV['PORT_MYAPP'] %>

4. Add static pages

In Nitrous console run:

rails generate controller Pages home

Next we need to change the homepage root so open your route file ~/myapp/config/routes.rb and replace

get "pages/home"

with

route :to => 'pages#home'

5. Test run the app

In Nitrous save all files and start up the server rails s from the app's root.

From the Preview dropdown select a port. Your browser should launch and show you Pages#home.

In the console enter Ctrl + c to stop the server.

Deploy to GitHub

6. Practice safe GitHubing with Figaro

We are going to setup a secure place to store sensitive variables. Right now lets protect our app's secret key.

In Nitrous on the file tree open gemfile and add gem "figaro", save, close.

In the console run bundle

Next up, use the generator provided by Figaro:

rails generate figaro:install

This creates a commented config/application.yml file and ignores it in your .gitignore.

Open ~\myapp\config\initializers\secret_token.rb

Copy out the secret key.

Open ~\myapp\config\application.yml. At the bottom enter your secret key in the following way:

SECRET_TOKEN: *your secret key*

Make sure to NOT include the ' at each end of the key.

Back in secret_token.rb change it to look like this:

Myapp::Application.config.secret_key_base = ENV['SECRET_TOKEN']

7. Configure Git

In Nitrous console set the username and email (make sure it is thae same as your GitHub user).

Check to see if it is already configured by running git config --global user.email. If your email apears then you are set and can ignore the next two commands. If no email shows then run the following commands.

git config --global user.name "YourName" change YourName

git config --global user.email example@YourEmail change example@YourEmail

Next in the console make sure your in the app's folder. Then run the following:

git init

git add .

git commit -am "Initial Commit"

8. Link Nitrous to GitHub

This requires a GitHub.com account so go make one.

In Nitrous go to Connections/Earn More N2O page (having tourble finding it? Click Here), click Connect GitHub. Go ahead and approve all prompts.

Next in Nitrous go to the Boxes page, select the box you are connecting to GitHub. Click Reveal Public Key then click Add to GitHub.

In GitHub click Account Settings in the top right. Goto SSH Keys and verify your Nitrous app appears in the list.

9. Get a repository and push to GitHub

While in GitHub on the top navigation click the "Create a new repo" button.

Fill out the form and Create Repository.

In Nitrous console cd into your project folder and run the below command. Modify it to match your app's repository URL.

git remote add origin git@github.com:username/myapp.git

git push origin master

Goto your repository's page on GitHub to see all the files that where uploaded.

10. How to push during development

In Nitrous save all your files then in the console run the following:

git add --all

git commit -am "description of change"

git push

Deploy to Production

11. Connect to a Heroku production dev box

We will be using the same login credentials you have for HerokuPostgres.

In Nitrous console login to your Heroku account by running:

heroku login

Then make a secure connection through SSH by running:

heroku keys:add ~/.ssh/id_rsa.pub

Start a new (and free) production box by running:

heroku create

Make note of your production URL.

12. Deploy to Heroku production

Push your app to Heroku by running:

git push heroku master

Since we used Figaro for ENV we need to run:

rake figaro:heroku

Congrats! Now you are ready to build your custom app.

@gamersaround
Copy link

Hello and thanks for the tutorial , it was a bit messy in my Heroku Apps/database but i finally got something running, i had to change this

Route.rb

root 'pages#home'

and not Route > ...

wasnt working, you can fix it if you want.
Thanks again for the other parts ;)

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