Creating a New Rails Project

July 11, 2008

A step by step guide to creating a new rails project. The project will be called np. We start with ...

rails np cd np

Setup GIT

git init

Remove log files

We want the log folder to be empty

rm -f log/*.log

Stop Git From Ignoring Empty Folders

We want git to check in all empty folders so we keep the project structure and don't get missing folder errors later. Unfortunately the only way we can do this is put a hidden file in each folder. We use a .gitignore file for this, and the following command puts one in each empty folder

find . \( -type d -empty \) -and \( -not -regex ./\.git.* \) -exec touch {}/.gitignore \;   

found that my blog is not showing the backslashes which are escaping each bracket in the above command, so try this one instead which doesn't use the brackets

find . -type d -empty -and -not -regex ./.git.* -exec touch {}/.gitignore \;    

Remove database.yml and make a template

mv config/database.yml config/database.yml.example

Make Initial Commit

git add .
git commit -a -m "initial commit"

Setup .gitignore

Because we now have no empty folders we can do this before the initial commit

mate .gitignore

Set contents to be


Commit changes

ga .

Test Clone

cd ..
git clone --bare np np.git
mv np np.old
git clone np.git
cd np
rake -T
script/console   # wil generate error about database.yml

Move Clone to Git Server

cd ..
scp -r np.git deploy@little-un:/srv/git/

Change Ownership of Clone on Server

sudo chown -R git:git /srv/git/np.git

Make Local Clone From Server

rm -rf np
git clone git@little-un:/srv/git/np.git

Setup Database

Each development environment will have its own database settings, and our production environments will in turn have their own secure settings. So we don't want to put database settings in our repository. We can put a template in our repository to guide setup.

git mv config/database.yml config/database.yml.example
git commit -a -m "removed database.yml from repo - added template"

Now each time you clone the repository you will have to setup an appropriate database.yml. Git will ignore this file because you told it to in '.gitignore'.

TIP Keep a local database.yml file on your development box that is configured for your local database settings. Use this as your template.

Capify and Setup Staging (Early Production) Environment

Doing this really early because I think its really important to do as early as possible. Really like to be able to push with git and then deploy.

capify .

Now you need to create a deploy.rb file. I already have one for my production environment, so its just a question of modifying it for this application.

We must modify

  • the application name (must not contain spaces)
  • the repository location

We can then check it by

cap deploy:check

This should product a couple of errors one telling you to run cap:deploy:setup. Make sure you get this (you don't want to overwrite an existing production app). Now setup and check again

cap deploy:setup
cap deploy:check

Now we should be ready for our first deployment. Be aware thought that we have to setup our web server before things will work. Also don't forget to commit changes to repo

ga .
gca -m "capified project and completed deploy:setup"

Web Server Setup

On web server ...

Again copying an existing setting. I'm using Apache and Passenger on Ubuntu. Existing sites are in /etc/apache2/sites-available. Create a new site by copying existing one and modifying. My new one came out as

<VirtualHost *>
        ServerName tc2.little-un
        ServerAdmin webmaster@localhost

        DocumentRoot /srv/rails-apps/tc2/current/public

Then as root

a2ensite tc2
apache2ctl configtest

n.b. should get warning about public folder not existing

Cap Cold Deploy

Back on dev box ...

Now may have done enough to do a cold deploy - which is a first deploy that does some additional stuff

cap deploy:cold 

Got error that database table did not exist, so created on server

Errors and Rails Version

Didn't have Rails 2.1 on my server when I did this. Can either freeze rails in application or update server. I'm updating the server for now! Probably should freeze.

Cap Deploy

Have to do at least one cap deploy to get my production database.yml setup (which is done by the capfile)

Just about done - got working production application. Now can get started with plugins and migrations