Liquid Digital Lab

Ruby on Rails Tutorial - An Online DVD Catalogue

Posted 3 years, 2 months ago by John

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:-

ss1.jpg

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:-

ss2.jpg

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).

ss3.jpg

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

Leave a Comment

Three D Clock

Classic clock built in processing

Posted by Lloyd - Comments (0)

This Happened #5

Lloyd gets chance to go along to the 5th gathering of This Happened

Posted by Lloyd - Comments (0)

Tom survives in one piece!

Tom makes it back to the office through 19 peaks over 149 miles, 60 flood warnings, 60mph gusts and 48hours of torrential rain, all in the name of MS Society, Wheels4Life and a weekend to remember!

Posted by Tom - 3 Comments (3)

Liquid goes Coast to Coast

Inspired by his managers previous fundraising wetsuit exploits, our newest designer Tom will be tackling the coast to coast bike ride this September.

Posted by Tom - Comments (1)

Liquid Artwork

Have a look at the artwork we created for the front office

Posted by Lloyd - 3 Comments (3)

Decisions, decisions… A Bit More Game AI

Game AI goes a few steps forwards, and 2000 years into the past…

Posted by Simon - 3 Comments (3)

Ever Won The Lottery?

Winning The Big One!
I recently read somewhere that winning the UK national lottery is easy. All you have to do is …

Posted by Simon - Comments (1)

3DS Max - Basics and Blueprints

The start of a series of tutorials in 3ds max from Liquids latest addition to the creative team.

Posted by Lloyd - 9 Comments (9)

Sound Reactive Sketch

We created this experimental Music Visualiser in processing & minim sound library.

Posted by Lloyd - 2 Comments (2)

Pathfinding in Games - Using A* in Director

Discover the joys of the A* pathfinding algorithm in our retro Director maze

Posted by Simon - Comments (1)

Colour Clock Screensaver

We’ve made an animated clock screensaver. You can have it too.

Posted by Ben - 2 Comments (2)

Using the Flash Tween Class

Trying to create realistic bouncing balls or elastic objects in Flash? Ben Stevens shows us the easy way to code motion using the Flash Tween Class.

Posted by Ben - 6 Comments (6)

A Jungle Jumble Using Lists in Director

What do you get if you cross a monkey, an elephant and a giant chicken-thing? Simon Edwards tries to explain using lists in Director.

Posted by Simon - 3 Comments (3)

Lloyd Survives the 2007 London Triathlon

Liquid’s very own budding triathlete successfully completes the Michelob ULTRA London Triathlon in a time of 2 hours and 42 minutes. Find out how he got on.

Posted by Lloyd - Comments (1)

Simple AI in Director

Simon Edwards discusses some of the techniques that developers call upon when programming games, particularly for defining logic that the computer will use for taking a turn in a 2-player game scenario.

Posted by Simon - Comments (0)

Ruby on Rails Tutorial - An Online DVD Catalogue

John Cove takes us through the process of making an online catalogue using RoR.

Posted by John - Comments (0)

Why write an ActiveX control?

Howard Peters discusses the advantages of using ActiveX, and explains how to create your own simple ActiveX control.

Posted by Howard - Comments (0)

Nichola’s Placement Year

Nichola, our resident student placement, has now been with us for a whole year and will be leaving us soon. So we asked her what it’s been like to work with the Liquid team.

Posted by Nichola - Comments (0)

Kinetica Soundwaves Exhibition

Exhibition visit

Posted by Ben - Comments (0)

PHP Tutorial - Creating a Quiz

John Cove explains how to use HTML and PHP to create an online quiz.

Posted by John - Comments (0)

Director Tutorial - Vector Font Outlines

Simon Edwards demonstrates an undocumented Director feature enabling you to map out a string of text in a vector cast member.

Posted by Simon - Comments (0)

HDR Photography

HDR (High Dynamic Range), is a software based, photographic technique that extracts tonal information from multiple exposures of the same shot. If you exposure bracket the same shot, you can merge all 3 images into one, then, using the data from all 3, create one output file which preserves accurate details from all exposures, bringing out highlights from the shadows.

Posted by Darren - Comments (0)

Happy Birthday To Us!

Liquid Digital Ltd has reached the end of its first financial year. There’s no time for cake and a candle though - there are some big things on the horizon for the first quarter of Year 2. Watch this space for news soon.

Posted by Simon - Comments (0)

Director Tutorial - Counting Down To A Launch Date

Tutorial written by our resident Director Guru, Simon Edwards.

Posted by Simon - Comments (0)

Lloyd in a wetsuit

Our creative leader will be braving the elements by competing in the Michelbob ULTRA London Triathlon on 4th August 2007.

Posted by Ben - Comments (0)