1.6. Whetting Your Appetite

A "Getting Started" chapter would not be complete without an example to show you how quickly applications can be prototyped thanks to Rails. The aim of this section is truly to "whet your appetite" as opposed to provide extensive explanations of each step. In Chapter 5, after covering the Ruby language in Chapters 3 and 4, you'll create a more complex Web application and everything will be explained in detail. Here I'm going to provide you with a sneak preview. After all, Rails became so popular thanks also to its ability to create entire applications with just a few commands.

Begin by creating a Rails project that can hold your friends' addresses. From within a directory of your choice (for example, c:projects), use the command prompt to run the following commands:

rails addressbook
cd addressbook

This generates an addressbook directory that contains the skeleton of your Rails application, and the second instruction makes it the current directory in your prompt.

This being Rails, you'll use a table within a database to store the data. This table will need fields like the person's name, address, phone number, and perhaps email and blog/site URL.

You can specify those fields and their data types thanks to the so-called scaffold generator, as follows:

ruby script/generate scaffold person name:string address:string phone:string
 email:string blog:string

This will generate a model, controller, and a series of view templates required to provide you with a basic application for performing CRUD operations.

Before you can see what it looks like, you will need to create a development database for the application as well as apply the table definition stored by scaffold in a migration file. The two tasks are achieved by a single command:

C:projectsaddressbook> rake db:migrate
(in C:/projects/addressbook)
==  CreatePeople: migrating ===================================================
-- create_table(:people)
   -> 0.0660s
==  CreatePeople: migrated (0.0680s) ==========================================

By convention the development database will be db/development.sqlite3; you didn't have to specify that nor provide a connection string. Also note how Rails is smart enough to figure out that a person's data should be stored in a people table (which is correctly pluralized).

Now that the database is taken care of, start the Web server:

ruby script/server

The Windows firewall may ask you to unblock the server, which you should agree to.

Point your browser to http://localhost:3000/people and you should see an interface for listing, creating, editing, and deleting entries in your address book. This also incidentally demonstrates Rails' cleverness in handling multiple versus singular terms, because the URL for the application is http://localhost:3000/people (where we'd expect to see a list of people). It's remarkable that no code was written to achieve all this.

Would you like to add validations to make the name and address mandatory? And how about ensuring that there are no duplicated names? Just add the following two highlighted lines within your person.rb model in the app/models directory:

class Person < ActiveRecord::Base
  validates_presence_of :name, :address
  validates_uniqueness_of :name
end

You now have a fully functioning, database-driven Rails Web application that has complete CRUD functionality. Into the bargain the application validates your input, checking that you have put in a name and address and checking that there are no duplicates (with nice friendly error messages if you get it wrong, as shown in Figure 1-12).

Figure 1.12. Figure 1-12

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.220.88.62