Now that we've set up our project, let's also set up GitHub so that we can save our project online and go back to previous versions when we want to.
First, we have to set up our SSH keys. Follow this guide to do so. Follow each step in the guide from "Checking for existing SSH keys" to "Testing your SSH connection".
After you've completed everything in that link, open up a second terminal window. You should now have 2 terminal windows open. One terminal window should be running the rails server.
Having Two Terminal Windows Open
We want to have two terminal windows open. This is because we want one for running the server and one for typing in rails commands.
You can create a new terminal window in C9 like this:
One terminal will look like this, where you can type in commands:
We also need another terminal window for running the server:
You'll notice that the terminal window that is running the server will not respond to anything that you type in. You can only execute commands in terminal windows that don't have a server running on it (the terminal window in the first screenshot).
In the terminal window for running commands, let's run the following command:
git init .
It should give you a message like this in the terminal:
Initialized empty Git repository in /home/ubuntu/workspace/.git/
Next, let's run the following commands:
git add .
git commit -am "Initial commit"
Now let's go into GitHub's front page and create a new repository. On the left-hand side, you should see a green button called "New".
Click on that button and just fill in the box that says "Repository name".
Make sure you don't check any inside "Initialize this repository with:"
Now click Create repository.
On the next page, go to the sections that says "…or push an existing repository from the command line".
Copy the code right beneath that and paste it into your terminal. It should look something like this:
git remote add origin git@github.com:YourGithubUsername/ideator.git
git push -u origin master
Make sure you replace
git@github.com:YourGithubUsername/ideator.git
with your username.
When you run this command, it might ask you for your username and password. If it does, then enter your Github username and Github password.
Once you are done with this process, let's make sure that everything is setup correctly. In your terminal, run the following command:
git remote -v
In your terminal window, the following lines should be outputted:
origin git@github.com:YourGithubUsername/ideator.git (fetch)
origin git@github.com:YourGithubUsername/ideator.git (push)
If nothing is outputted, then chances are that you missed a step. Try following every step carefully and make sure not to make typos.
You're all set! Your GitHub is set up and ready to go!
Heroku is a service that lets you deploy your application live extremely easily. Professional teams use Heroku because of how easy it is to use it. It allows you to push your code live in no time!
First, go to Heroku and create a new Heroku account.
Next, in your C9 terminal, enter the following command:
wget -O- https://toolbelt.heroku.com/install-ubuntu.sh | sh
This command will install and update the Heroku Toolbet, which is necessary to deploy your applications on to Heroku from the command line.
Once it is finished installing, open up your Gemfile
in C9. You can find this file by pressing CTRL + P
or CMD + P
on Mac, then typing Gemfile
.
There are several types of databases, such as MySql, sqlite3, Postgresql. Heroku only supports Postgresql, so we need to change our settings so that our application runs on Postgresql.
In the Gemfile
, find the following lines of code:
# Use sqlite3 as the database for Active Record
gem 'sqlite3'
Replace sqlite3
with pg
. Your Gemfile
should now look like this:
# Use pg as the database for Active Record
gem 'pg'
Next, in your terminal window, run bundle install
.
The bundle install
command will install the gems included in the Gemfile
(more on gems later).
Next, open your database.yml
file. Again, database.yml
holds the database configuration for your application.
Replace the file contents with the following lines of code:
default: &default
adapter: postgresql
encoding: unicode
pool: 5
username: username
password: password
host: <%= ENV['IP'] %>
development:
<<: *default
database: ideator_development
test:
<<: *default
database: ideator_test
production:
<<: *default
database: ideator_production
Next, let's first commit (save) the changes we have made to GitHub. In the terminal, run the following commands:
bundle install
git add .
git commit -am "change database to postgresql"
git push origin master
Next, in your terminal window, run the following command (make sure there is a dollar sign):
sudo service postgresql start
Next, run this command:
sudo sudo -u postgres psql
You will see a new console appear. Instead of the $
sign, you should see postgres=#
Inside this console, type in the following command:
CREATE USER username SUPERUSER PASSWORD 'password';
Next, type in the following commands:
UPDATE pg_database SET datistemplate = FALSE WHERE datname = 'template1';
DROP DATABASE template1;
CREATE DATABASE template1 WITH TEMPLATE = template0 ENCODING = 'UNICODE';
UPDATE pg_database SET datistemplate = TRUE WHERE datname = 'template1';
\c template1
VACUUM FREEZE;
Next, quit of of the postgres console by running this command:
\q
Once we've finished these steps, we're ready to deploy our new application to Heroku.
To create a new application, we can run the heroku create
command.
heroku create app_name
Change app_name
to the name of the application.
If it asks you for your Heroku credentials, enter your email address and password.
As you can see, it creates a url that anyone can visit. For example, take a look at the above screenshot (https://ideator-app.herokuapp.com/
).
Now let's push our application to Heroku.
In the terminal, enter the following command:
git push heroku master
It will take some time for everything to be uploaded to the Heroku server. Once it has finished uploading, we need to migrate our database. In the terminal, enter the following command:
heroku run rake db:migrate
After this step is finished, our last step is to restart our server. We can do this by running the following command in the terminal:
heroku restart
Now if you go to the application URL, you should see a new Rails application deployed. Congratulations, you've deployed your first Rails application!