DatabaseSkip the CRUD: Building a Simple Database Backed Web Application Using Ruby...

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

Developer.com content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

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

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:

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:

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 myfirstrailsappconfigenvironment.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 myfirstrailsappconfigdatabase.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 FilesMySQLMySQL Server 5.0bin on my system) into the Ruby installation bin directory (C:rubybin 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:

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:

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

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

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

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

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

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.

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories