Wednesday, December 26, 2012

Guide to start a Rails application with Heroku

There is a previous post with my notes as a Guide to start a Rails application. This is a similar post but adding how to deploy the application to Heroku.

Let's start creating the application with Rails:
#Create the application
rails new DEMO
#Execute the server
rails s
#Open in a browser localhost:3000
cd DEMO
#If I'm in Mac OS
mate .
#If I'm in Linux or Windows I use SublimeText2
#subl .
#Create a controller to be the front page of the application
rails g controller welcome index
#Delete the index.html page, if not, the server will continue redirecting
rm public/index.html
Open the file /config/routes.rb and uncomment this line. This will route all the access to / (root) to the index page of the welcome controller.
root :to => 'welcome#index'
As Heroku do not use SQLite, replace SQLite by PostgreSQL in the Gemfile. Open /Gemfile and modify this line:
# gem 'sqlite3'
gem 'pg'
Install the new gem executing this.
bundle install
Heroku has a git service for version control but I also like Github so let's use both Go to github.com and create a repository with the name of the application (DEMO). Do not create a README neither check any box.
#Create the README file with the description of your project.
echo "Description of your application" > README.md
#Initialize the git project
git init
#Add everything and commit, this will not sync your repository.
git add .
git commit -m "First commit"
#You may get this line from github when you create the repository.
#Replace my username with yours.
git remote add origin git@github.com:johandry/DEMO.git
#Synchronize or, in this case, send everything to your repo
git push origin master
Before use Heroku, it needs to be installed as well as Toolbelt, check other post in this blog to know how to do it in Linux and OS X.
Login to heroku with Toolbelt, setup Git and deploy the application.
$ heroku login
Enter your Heroku credentials.
Email: email@example.com
Password (typing will be hidden): 
Authentication successful.
$ heroku create
Creating shrouded-ocean-6001... done, stack is cedar
http://shrouded-ocean-6001.herokuapp.com/ | git@heroku.com:shrouded-ocean-6001.git
$ git push heroku master
In this case when you push to Origin the commit will go to Github and when you push to Heroku the commit will go to ... Heroku :).
Replace the push lines with:
# Push the commit to Origin, that is Github
# this is the same as git push origin master
$ git push 
# Push the commit to Heroku
$ git push heroku master
Now you can create the first Scaffold and continue with the next steps from the previous Guide to start a Rails application.
Post a Comment