Ruby is the programming language on the tip of everyone's tongue these days. The power and ease of software development with Ruby has helped make an upstart web framework called Ruby on Rails the hottest web framework around. Some may argue that the Web 2.0 wave is riding the Rails on Rails wave as more and more Ruby on Rails powered web sites pop up on the Internet.
This article will give a brief introduction to Ruby on Rails and show you how to get your first simple Ruby on Rails web application up and running. In Part 2 of this article, you will connect your web application to the MySQL database and add CRUD (Create, Read, Update, and Delete) support to the web application.
Ruby: A Scripting Language Gem
It is hard to imagine anyone in the programming world these days who has not heard of Ruby. The ever-increasing popularity of the Ruby on Rails web framework is helping to make Ruby the language of choice for rapid application development and testing. Ruby is an interpreted scripting language that provides quick and easy object-oriented programming and contains some neat features such as closures, blocks, and mixins. Ruby is also highly portable, running on Unix/Linux, Windows, and MacOS. For those wanting a more thorough introduction to Ruby, you can read W. Jason Gilmore's article on Ruby.
Rails: The Fast Train to Web Application Development
Ruby on Rails was created by David Heinemeier Hansson, a partner of 37 signals, as a web application framework to build their web applications. David open sourced Ruby on Rails under the MIT license and it is now the fastest-growing web application framework. It is a full-stack framework for developing database-backed web applications according to the Model-View-Control pattern. It provides Ajax support in the view layer, a request and response controller, and a domain model wrapping the database. All that is needed to deploy a Rails web application is a web server and a database. In this article, you will download and install Ruby, the Rails framework, and MySQL so that you can run your very first Ruby on Rails web application.
Let Us Recap (Getting Ruby on Rails up and running)Let Us Recap (Getting Ruby on Rails up and running) In my previous Ruby on Rails article, you downloaded and installed the latest versions of Ruby, Rails, and MySQL. You then saw how to install each piece of software. Finally, you generated your first Rails application and got it up and running.
In that previous article, you downloaded and installed the latest versions of Ruby, Rails, and MySQL. You then saw how to generate your first Rails application and get it up and running.
Updating to the Latest Version of Ruby on Rails
In the previous article, you installed that latest version of Rails, which was Rails 1.1.6. Since then, a new version of Rails has been released (Rails 1.2.1). Let's check your current version of Rails, and then perform an upgrade, to see how simple and easy this is. First check your current Rails version. Enter the command rails -v:
As you can see, you are running Rails 1.1.6.
Now, see what Ruby Gems you have installed on your system. Enter the command gem list --local:
This gives you a nice summary of the current Gems installed on your system. Now let us upgrade Rails to the current version (you can always visit the Ruby on Rails home page to see what the current version of Rails is). To perform the upgrade, enter the command gem install rails -include-dependencies:
You are now upgraded to the latest Rails 1.2.1 version.
Creating Your Database Table
Now, you are ready to create the database table that Rails will manipulate. I will use the MySQL Query Browser to administer the MySQL database and create the schema and table that I will use. You first create a database (schema) named 'rails':
Now that you have created your database, you can now create your table. Because I play and collect guitars, I would like to use 'myfirstrailsapp' to keep track of the current guitars that I own. I will define a 'guitar' as having the following attributes:
|Make||Fender, Squier, Gibson, Epiphone|
|Model||Stratocaster, Les Paul|
|Color||Black, Fiesta Red, Cherry Sunburst|
So, create your 'guitars' table. By default, Rails expects the following two conditions to be satisfied for all database tables that it has access to:
- The table name is plural.
- A column named 'id' exists and is the primary key column for the table.
Here is what the definition for your 'guitars' table looks like:
A nice feature of MySQL Query Browser is that when you click 'Apply Changes', it prompts for confirmation of the SQL that it plans to execute. You can copy this SQL to a text file if you ever need to re-run these steps by hand:
You now have your 'guitars' table created and are ready to have Rails create CRUD operations for it. However, before you can get to this step, you need to perform some preparation steps.