September 3, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

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

  • January 30, 2007
  • By Dominic Da Silva
  • Send Email »
  • More Articles »

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.





Page 2 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel