Ruby on Rails, often shortened to RoR, is a web application framework. The framework aims to reduce the about of time associated with common web development activities. For example, numerous frameworks provide libraries for session management, database access and templating frameworks. The aim of RoR is to increase the speed and ease of database driven sites, migration and modification of databases and is able to create skeleton code frameworks, affectionately known as scaffolding!
This tutorial assumes you have some prior knowledge of web servers, command line entry and mySQL. The tutorial will outline the installation and the creation of a basic skeleton framework. The tutorial will assume a web server, such as Apache or IIS, and mySQL 5 is installed, configured and running on the local machine.
The first step is to install RoR. You can download an installer from the official RoR web site (http://www.rubyonrails.org). Once this has been installed, download and extract RubyGems, the standard Ruby package manager, from the same web site.
Extract the file and navigate to the extracted path. From command line type:-
ruby setup.rb
With RubyGems successfully installed, you can download and install all of Rails and the dependencies.
Again, from the command line type:-
gem install rails --include-dependencies
Congratulations, you now have RoR installed.
The next step is to log into mySQL:-
mysql -h {ip address} -u root -p
Enter password: ********
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1 to server version: 5.0.21-community-nt
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql>
The next step is to create a new database and three tables within the database, as below:-
mysql> create database dvds_development;
Query OK, 1 row affected (0.00 sec)
mysql> create database dvds_production;
Query OK, 1 row affected (0.00 sec)
mysql> create database dvds_test;
Query OK, 1 row affected (0.00 sec)
There is a naming convention for the databases. These are development, production and test. They relate to the various phases of the applications’ development. Each phase of development is set within the RoR environment.
For this tutorial the dvds_production and dvds_test databases will not be used. The next step is to create a table in the dvds_development database.
mysql> USE dvds_development;
mysql> CREATE TABLE `dvds` (
-> `id` SMALLINT(5) UNSIGNED NOT NULL AUTO_INCREMENT,
-> `title` VARCHAR(255),
-> `description` VARCHAR(255),
-> `genre` VARCHAR(30),
-> PRIMARY KEY (`id`)
-> );
Query OK, 0 rows affected (0.06 sec)
The next step is to create the project space and scaffolding.
From the command line, navigate to where the application will reside and type:-
rails dvds
This creates the RoR framework for the DVD collection application. Various folders and files are created. Most of the time is spent in the app, db and public folders.
If all went well, the following will appear:-
Z:\Ruby>rails dvds
create
create app/controllers
create app/helpers
create app/models
create app/views/layouts
create config/environments
create components
create db
create doc
create lib
create lib/tasks
create log
create public/images
create public/javascripts
create public/stylesheets
create script/performance
create script/process
create test/fixtures
create test/functional
create test/integration
create test/mocks/development
create test/mocks/test
create test/unit
create vendor
create vendor/plugins
create tmp/sessions
create tmp/sockets
create tmp/cache
create tmp/pids
create Rakefile
create README
create app/controllers/application.rb
create app/helpers/application_helper.rb
create test/test_helper.rb
create config/database.yml
create config/routes.rb
create public/.htaccess
create config/boot.rb
create config/environment.rb
create config/environments/production.rb
create config/environments/development.rb
create config/environments/test.rb
create script/about
create script/breakpointer
create script/console
create script/destroy
create script/generate
create script/performance/benchmarker
create script/performance/profiler
create script/process/reaper
create script/process/spawner
create script/process/inspector
create script/runner
create script/server
create script/plugin
create public/dispatch.rb
create public/dispatch.cgi
create public/dispatch.fcgi
create public/404.html
create public/500.html
create public/index.html
create public/favicon.ico
create public/robots.txt
create public/images/rails.png
create public/javascripts/prototype.js
create public/javascripts/effects.js
create public/javascripts/dragdrop.js
create public/javascripts/controls.js
create public/javascripts/application.js
create doc/README_FOR_APP
create log/server.log
create log/production.log
create log/development.log
create log/test.log
The mySQL database connection for the new application needs to be set up. This is unique to the application. Edit the database.yml file within the config directory of the dvds application.
Replace the following text:-
development:
adapter: mysql
database: dvds_development
username: root
password:
host: localhost
with:-
development:
adapter: mysql
database: dvds_development
username: root
password: {password}
host: localhost
socket: /tmp/mysql.sock
There are a few things to take note of here. The {password} type should be changed to the root user’s password on the mySQL database server. The new line, socket, is the TCP/IP socket file for the mySQL connection. On Windows this will most certainly be /tmp/mysql.sock. It will be different if on a Macintosh.
From the dvds application directory, in this case Z:\Ruby\dvds, the following should be entered to create the model, controller and view files:-
ruby script/generate scaffold dvd
If all went well, the following will appear:-
Z:\Ruby\dvds>ruby script/generate scaffold dvd
exists app/controllers/
exists app/helpers/
exists app/views/dvds
exists app/views/layouts/
exists test/functional/
dependency model
exists app/models/
exists test/unit/
exists test/fixtures/
create app/models/dvd.rb
create test/unit/dvd_test.rb
create test/fixtures/dvds.yml
create app/views/dvds/_form.rhtml
create app/views/dvds/list.rhtml
create app/views/dvds/show.rhtml
create app/views/dvds/new.rhtml
create app/views/dvds/edit.rhtml
create app/controllers/dvds_controller.rb
create test/functional/dvds_controller_test.rb
create app/helpers/dvds_helper.rb
create app/views/layouts/dvds.rhtml
create public/stylesheets/scaffold.css
It is now time to bring RoR online. From the command line, within the dvds application folder, the following should be entered:-
ruby script/server -port=81
and:-
Z:\Ruby\dvds>ruby script/server --port=81
=> Booting WEBrick…
=> Rails application started on http://0.0.0.0:81
=> Ctrl-C to shutdown server; call with –help for options
[2007-06-07 12:26:29] INFO WEBrick 1.3.1
[2007-06-07 12:26:29] INFO ruby 1.8.6 (2007-03-13) [i386-mswin32]
[2007-06-07 12:26:29] INFO WEBrick::HTTPServer#start: pid=2736 port=81
From a web browser, browse to:-
http://localhost:81/
The RoR Welcome aboard sign will be displayed. The dvds application will reside here:-
http://localhost:81/dvds
The following should be on-screen:-

From this page the DVD collection can be managed and made available to all your friends, family and colleague. Maybe you could start up a DVD swapping club?? Ooh, the possibilities!!!
Click on the “New dvd” link to create a new dvd:-

Once the DVD information has been entered and the Create button pressed, the entry will be added to the mySQL database and the main DVD screen will be displayed showing the DVD information in the database, including the title that has just been added.
Each DVD entry can be displayed, edited or deleted (destroyed).

This tutorial only touches the tip of the iceberg. There are plenty of tutorials on the Internet if you want to explore further.







