Tech Journal Back to Tech Journal

Setting up another Ruby on Rails application

Setting up a ruby on rails project, after you already have a working vhost in apache that runs ruby on rails:

1. Create the rails application dir:

$ rails /path/to/myapp

2. Add a vhost into apache, by addding into httpd.conf:

<VirtualHost *:80>
    DocumentRoot "/path/to/myapp/public"

    # the following added for Ruby on Rails
    SetEnv RAILS_ENV development
    <Directory /path/to/myapp/public>
        Options ExecCGI FollowSymLinks
        AddHandler cgi-script .cgi
        AllowOverride all
        Allow from all
        Order allow,deny

3. Fix permissions so that apache can write into tmp dir, for instance by doing:

$ chmod 777 /path/to/myapp/tmp -R
$ chmod gu+s /path/to/myapp/tmp

4. Create 3 databases: myappdb_test, myappdb_production, myappdb_development
(the production one will be used when the app in set to production. In step 2 we actually set it to development. the test db is used by rake for testing)

5. Edit /path/to/myapp/conf/database.yml to specify the above 3 databases for the 3 modes: development, test, production. And also specify the user, password & etc. required to use the DB server.

6. Create tables in the development DB (if you're using postgresql, create the same tables in all 3 DBs). These tables are the models stored in the DB. For instance:

mysql> use myappdb_production;
mysql> create table people (
    -> id int(10) unsigned not null auto_increment,
    -> name varchar(50) not null default '',
    -> street1 varchar(70) not null default '',
    -> street2 varchar(70) not null default '',
    -> city varchar(70) not null default '',
    -> state char(2) not null default '',
    -> zip varchar(10) not null default '',
    -> primary key (id),
    -> key name (name)
    -> ) type=MyISAM auto_increment=2;
mysql> insert into people values (1, 'Superman',
    -> '123 Somewhere', '', 'Smallville', 'KS',
    -> '123456');

7. Create an MVC. For instance:

$ ./script/generate controller Friends list view new edit
$ ./script/generate model Person

8. Test it all:

$ rake

after generating a model, you can look in ./db/migrate/001_create_modelname.rb, and modify self.up to define how the table is created, and run rake db:migrate.

Last updated on 2007-08-25 14:28:10 -0700, by Shalom Craimer

Back to Tech Journal