http://www.developer.com/

Back to article

Skip the CRUD: Building a Simple Database Backed Web Application Using Ruby on Rails


January 30, 2007

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:



Click here for a larger image.

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:



Click here for a larger image.

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:



Click here for a larger image.

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



Click here for a larger image.

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:

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

  1. The table name is plural.
  2. 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:



Click here for a larger image.

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.

Preparing Your Rails Application to Use MySQL

There are three things we need to do to our 'myfirstrailsapp' before we can generate the CRUD operations for our 'guitars' table:

  1. Update 'myfirstrailsapp' to specify that it will use Rails 1.2.1.
  2. Point 'myfirstrailsapp' to our MySQL database.
  3. Make sure our MySQL driver file is available to Ruby.

Updating 'myfirstrailsapp'

To update 'myfirstrailsapp' to Rails 1.2.1, you must edit the myfirstrailsapp\config\environment.rb file. Comment out the line referring to Rails 1.1.6 and add a new line specifying Rails 1.2.1:

# Specifies gem version of Rails to use when vendor/rails is not
# present
# RAILS_GEM_VERSION = '1.1.6'
RAILS_GEM_VERSION = '1.2.1'

Pointing 'myfirstrailsapp' to your MySQL database

To point 'myfirstrailsapp' to your MySQL database, edit the myfirstrailsapp\config\database.yml file. This file contains database configurations for the 'development', 'test', and 'production' environments of your Rails application. Edit the 'development' section and specify your database configuration:

development:
   adapter:  mysql
   database: rails
   username: root
   password: <your DB password here>
   host:     localhost

Making sure your MySQL driver file is available to Ruby

To ensure that your MySQL driver file is available to Ruby, you need to copy the libmySQL.dll driver file located in your MySQL installation bin directory (C:\Program Files\MySQL\MySQL Server 5.0\bin on my system) into the Ruby installation bin directory (C:\ruby\bin on my system). Ruby (and therefore Rails) will use this driver file to access MySQL.

Creating CRUD with Rails

You now can generate the CRUD operations for your 'guitars' database table. To do this, you tell Rails to generate the 'scaffold' for your 'guitars' table. Rails will generate Model, View, and Controller classes within the Rails application and CRUD operations to access the 'guitars' table. Issue the following to have Rails generate the scaffold:



Click here for a larger image.

As you can see, Rails generated a guitars directory in the application's views folder, a guitars.rhtml file in the views/layouts folder, a guitars_controller.rb file in the controllers folder, and a guitar.rb file in the models folder.

You now can start the 'myfirstrailsapp' web application by entering the following command rails ruby scriptserver:



Click here for a larger image.

Point your browser to http://localhost:3000/guitars. You will see an (empty) listing of the 'guitars' database table:



Click here for a larger image.

You now can enter a new 'guitar' by clicking the 'New Guitar' link, entering your first guitar's information, and clicking the 'Create' button:



Click here for a larger image.

You are sent back to the listing page, which shows your newly added guitar entry, along with a confirmation message:



Click here for a larger image.

Look at the database and confirm that you have this new record in the 'guitars' table:



Click here for a larger image.

After entering a few more guitars, you have the following listing:



Click here for a larger image.

You also can view and edit an entry by clicking the 'Show' and 'Edit' links.

Rails has generated all the CRUD operations for the 'guitars' table, and provided you with a web-based interface to manipulate its data. The beauty of Rails is all of this took place without having to write one line of Ruby code!

Conclusion

This second article in my Ruby on Rails series has shown you how to connect your web application to the MySQL database and add CRUD (Create, Read, Update, and Delete) support to the web application. Rails provides the scaffolding to get you started with CRUD operations, leaving you with the task of enhancing your application with the business rules. That is the beauty of Rails; it follows the DRY (Don't Repeat Yourself) principle, effectively making the easy things simple, and giving you more time to work on the hard stuff. In the next installment of this series, you will enhance your application's user interface and get your hands dirty with some Ruby coding.

References

About the Author

Dominic Da Silva (http://www.dominicdasilva.com/) is the President of SilvaSoft, Inc., a software consulting company specializing in Java, Ruby, and .NET-based web and web services development. He has worked with Java since the year 2000 and is a Linux user from the 1.0 days. He is also Sun Certified for the Java 2 platform. Born on the beautiful Caribbean island of Trinidad and Tobago, he now makes his home in sunny Orlando, Florida.

Sitemap | Contact Us

Thanks for your registration, follow us on our social networks to keep up-to-date