|Figure 5. Creating the Contact Type Model Class: Running the Ruby on Rails script "script\generate model ContactType" created a new Rails model class called ContactType in a file called contact_type.rb under the rolodex\app\models folder.|
Your First Rails Application
With the database configured in Rails, you are now ready to build your first application. Rails promotes the development of Web applications using MVC architecture. In this example, you'll create your application by creating a model and working your way to the view. As you create this application, count the number of lines of code you have to write or even modify to get the application up and running.
To begin, let's build simple some Web pages to create and modify contact types. Contact types, such as Friend, Coworker, Family, Business Associate, etc., are simple reference data used to type the contacts stored in the Ruby Rolodex database. The contact types will be stored in the contact_types database table. It is time to use one of Rails' metaprogramming scripts to build the first components to display and edit contact types.
From the \rolodex directory in a command prompt, issue the following command:
ruby script\generate model ContactType
The output of this script is shown in Figure 5
. It results in a new Ruby "MVC" model class being created in the rolodex\app\models
directory called contact_type.rb
|Figure 6. Creating the Contact Type Controller Class: Running the Ruby on Rails script "script\generate controller ContactType" created a new Rails controller class called ContactTypeController in a file called contact_type_controller.rb under the rolodex\app\controller folder.|
If you open contact_type.rb file with a text editor, you won't find anything that looks too exciting. In fact, it doesn’t look like this model class does anything at all.
class ContactType < ActiveRecord::Base
Actually, this Ruby class now represents instances (rows) from the contact_types
database table. You will see this more in a bit. By the way, in case you were not keeping track, that's zero (as in zip, nada, none) lines of code you have had to write so far.
|Figure 7. Starting the WEBrick Server: The WEBrick Web server is included with Ruby on Rails. Results of starting the server are displayed on the left. Results of hitting the Web server from a browser (http://localhost:3000) are displayed on the right.|
On to building the controller class in the MVC framework and again, time for another Rails script. At the command prompt, type the following command:
ruby script\generate controller ContactType
depicts the results of running this command. You will note that, this time, the script generated a file (contact_type_controller.rb
) in the rolodex\app\controller
You won't find much in the contact_type_controller.rb
file eitherit's pretty much just a class definition again. This time, however, you will have to add a line of code. Open the file and add the scaffold :contact_type
line. The full class is listed below:
class ContactTypeController < ApplicationController
Still keeping score? That's a whopping one line of code you had to write. Counting changes in the database configuration file, that's one line of code and a couple of configuration edits. And, my friend, that is it. You don't have to do anything for the view side of this MVC application.
|Figure 8. Rails Web Application for Contact Types: A complete Web site for adding, editing, showing, and removing contact types all developed with Rails script execution and one line of code.|
Running Your First Rails Application
Ruby on Rails is both a Web application development environment as well as a complete Web runtime environment. It ships with a built-in Web server called WEBrick. The WEBrick server is built in to each Rails project created using the rails script you used above to create your project. To start the Web server, open another command prompt and go to the rolodex directory. Then enter the following command to start the Web server:
Bring up a browser and enter http://localhost:3000
to test your server. You should see the Ruby on Rails welcome page as shown in Figure 7
Now, try out your contact type Web pages by entering the following URL: http://localhost:3000/contact_type/list
. You can say it now…WOW! That's right. With one line of code, a couple of configuration changes and some execution of Ruby on Rails' metaprogramming scripts, you now have a little Web application to enter, modify, and remove contact types for your Ruby Rolodex application. If you preloaded the database from the SQL script provided, you should pages displayed that look similar to those displayed in Figure 8