Ruby on Rails Quick Start
Ruby On Rails – From Nothing To A Simple Database Driven Web Application In Ten Simple Steps
This is a quick tutorial for jumpstarting a simple, flat, one table, database-driven web application using RubyOnRails. This tutorial assumes that you have neither Ruby nor a database installed on your machine. You don’t even need to know Ruby. Let’s go!
Note: this was originally written for the 1.0 release of Rails. I’ll be updating it soon to better reflect the changes for Rails 2.x
Step 1 – Install Ruby
If you do not have Ruby 1.8.2 or greater on your machine, go get it from
http://www.ruby-lang.org/en/downloads/
Mac and Linux users, you probably already have Ruby installed, Windows users you can get the installer from http://rubyinstaller.rubyforge.org. Install the installer, use all of the defaults.
Step 2 – Install Rails
Open a command line terminal. Type:
gem install rails --include-dependencies
Step 3 – Install MySQL
Download an installer from http://dev.mysql.com/downloads/mysql/.
Unzip the download and run the appropriate installer. Feel free to stick with the defaults. You will probably be asked to set the root password, so set it and don’t forget it!
Step 4 – Create a MySQL Database and Grant Permissions On It
Let’s call our project zabada. So following the Rails conventions, the database for development will be named zabada_development
In a command line terminal, log into MySQL:
mysql -u root -p Enter Password: *****
Now, from the MySQL shell, do this:
mysql> create database zabada_development; mysql> grant all on zabada_developement.* to 'rlambert'@'localhost'
Obviously, rlambert is my user name on my computer. Use your own user name when you do this.
On Windows, I had to also grant permission to ‘ODBC’@’localhost’, you might have to do this:
mysql> grant all on zabada_developement.* to 'ODBC'@'localhost'
Step 5 – Build the Rails Project
In a command line terminal, go to an empty folder where you want to build Rails projects. Then type:
rails zabada
Change to the newly created Rails “zabada” project directory:
cd zabada
For the rest of the steps, keep your terminal open and stay in this directory for all further commands.
Step 6 – Create Your Schema for Your Database Table
For our simple project, we’ll persist simple widget entities. Under the “zabada” directory, there is a “db” directory. Create a file in the “db” directory called “create.sql”.
zabada> db/create.sql
Create your table schema in that file. For example:
drop table if exists widgets; create table widgets ( id int not null auto_increment, name varchar(100) not null, size decimal(10,2) not null, date_available datetime not null, description text not null, primary key (id) );
Feel free to add columns, or switch up the names of the columns if you want. Rails won’t care. It is important to use the name widgets for the table name though. By default, each Rails class assumes that it maps to the plural of the class name.
Now load this schema into our MySQL database:
mysql zabada_development <db/create.sql
Step 7 – Configure the Database for Rails
Edit the file config/database.yml with the appropriate connection info for your “zabada_development” database.
development: adapter: mysql database: zabada_development username: root password: YOURPASSWORD socket: /path/to/your/mysql.sock
Step 8 – Generate the Widget Admin Scaffold
Type:
ruby script/generate scaffold Widget Admin
Step 9 – Start Up the Built-In Web Server
ruby script/server
Step 10 – Open a Browser and Play!
Open you favorite web browser and go to:
http://localhost:3000/admin
You should be able to create and edit Widgets! It is not super pretty yet, but we’ll work on that when we have more than 10 steps to work with!
Bonus – Step 11 – Add Simple Validation
Edit the file app/models/widget.rb
Let’s make several fields required. Add :
validates_presence_of :name, :size, :date_available
Let’s also make sure that the name is unique:
validates_uniqueness_of :name
Finally, let’s make sure that the size is a valid number:
validates_numericality_of :size
Your file should now look like:
class Widget < ActiveRecord::Base validates_presence_of :name, :size, :date_available validates_uniqueness_of :name validates_numericality_of :size end
Save the file, and go back to your browser. The validations should be in place. Create some new Widgets and test it out.
Next Steps
Customize the Look and Feel
Edit the *.rhtml files in app/views/admin and app/views/layout. Feel free to tweek the html to your liking.
Edit the CSS file at public/stylesheets/scaffold.css. Maybe it would be easier to see the list page if the table cells had borders, add:
td
{
border: 1px solid #000;
}
Read On!
- Check out all the latest Rails tutorials and documenation at http://www.rubyonrails.org
- Buy the Pragmatic Programmers’ fantastic book Agile Web Development with Rails, authored by the excellent Dave Thomas and the lead developer of Rails, David Heinemeier Hansson.
Check out the screenshots below.
Attachments:
| zabada-rails-widget-list.g | 42260 bytes |
| zabada-rails-edit-w-validation.gif | 36426 bytes |



