Articles

Building First Rails Application

Introduction

Now that we know what Ruby, Rails and what Rails made of, we can start putting together a delicious recipe which will be as quick as an apple pie, as easy as a pancake.

The Agenda

Keeping things simple and straightforward, we will create an employee information manager, in which we would be able to enter, edit and maintain a simple address book of a company's employees.

The Process

Create a blank Rails application with the default directory structure. Type the following command on your windows command line:

 

C:> rails eim

A host of files and directories will be created for you as shown in the figure 1.



Click here for larger image

Figure 1

Now change the directory to the application directory by typing:

 

C:> cd eim
C:eim>

We need to have a database in order to store, edit and maintain our employee details. Our choice of database is MySQL for this tutorial. Login to your MySQL query browser and create a fresh blank database.


Figure 2


Figure 3

Navigating to the config folder of the application, open database.yml using the editor of your choice and edit it to make it work for MySQL database. The default database.yml file generates for SQLite. The database.yml for MySQL it looks like this:

 

development:
adapter: mysql
database: eim_development
username: root
password: rails
host: localhost
test:
adapter: mysql
database: eim_test
username: root
password: rails
host: localhost
production:
adapter: mysql
database: eim_production
username: root
password: rails
host: localhost

Change the database credentials according to your local environment. As mentioned in our previous article, the tables in Rails are defined using migrations which makes the application database ultra-portable. In order to create the migration, open the command line and type the command:

 

C:eim> ruby script/generate migration employee



Click here for larger image

Figure 4

This command generates a folder named migrate under your db directory and a file named 20091028212040_employee. This file's name consists of a datetime stamp and the mentioned tablename in the command. Let's check what information we want from our employees. Lets keep it simple, name, designation and address. Here is our table definition inside the migration file.

 

  class Employee < ActiveRecord::Migration
def self.up
create_table :employees do |t|
t.string :name
t.string :designation
t.text :address
t.timestamps
end
end
def self.down
drop_table :employees
end
end

The first part of the definition (t.string :name) defines the data type and the second part provides the name of the field. Once all the definitions have been made, the database creation is just one command away.

 

C:eim> rake db:migrate


Figure 5

As shown in the figure above, the tables are created on the successful running of migration. We can go and check out our query browser.


Figure 6

With these tables ready, we are ready to take a leap. We will generate a scaffold of the Rails application. Just to refresh, the scaffold generates, models, controllers and default views. It adds Restful routes to the controllers and writes all the actions by default.

At the stroke of this magic command we will have the structure of our application developed:

 

C:> ruby script/generate scaffold employees


Figure 7

Everything will be setup except your views, so lets go and edit our views. They reside in the app/views/employees directory. We will add the field names and data types in order to display the respective type of field.

 

  <h1>New employee</h1>
<% form_for(@employee) do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
<%= f.label :designation %><br />
<%= f.text_field :name %>
<%= f.label :address %><br />
<%= f.text_area :address %>
</p>
<p>
<%= f.submit 'Create' %>
</p>
<% end %>
<%= link_to 'Back', employees_path %>

Also, to see each record, we have a show action defined. In our show.html.erb, we add the following:

 

  <b>Name:</b>
<%=h @employee.name %>
<b>Designation:</b>
<%=h @employee.designation %>
<b>Address:</b>
<%=h @employee.address %>
<%= link_to 'Edit', edit_employee_path(@employee) %> |
<%= link_to 'Back', employees_path %>

In the above HTML snippet, an instance variable of employee is created and each attribute of the employee object is called in each field to display the field.

Let's see how our application looks and works so far. Boot the server with the command:

 

C:eim> ruby script/server

Navigate to http://localhost:3000/employees/new on your locahost in your browser and you will be presented with the form we just created.


Figure 8

On filling the details in the form, and pressing create, we will be taken to the show page.



Click here for larger image

Figure 9

As seen in the browser, the URL is a Restful one as it points towards the resource directly.

That was pretty fast wasn't it?

Adding Validations

Validations in Rails are pretty much like English sentences and are defined inside the model. Let us consider a case, where we want don't want any of the fields empty and all the fields to be compulsory. Our validation would look like this:

 

class Employee < ActiveRecord::Base
validates_presence_of :name, :designation, :address
end

In the above validation, we check weather the fields name, designation and address is filled out or not. If left empty, it will throw an exception which looks like this:


Figure 10

There are a long list of validations which check the uniqueness of the field, regular expressions for email syntax, and nested validations too.

Conclusion

Now, that we have created a basic employee information system, we will enhance it further in the coming tutorials by adding a login system and some cool AJAX features to it.

 

original article


Website Design | Website Development | Flash animation | Flex applications | Logo Design | Mobile Applications Development | iPhone | iPad | Google Android

Content Management Systems | E-Commerce | Social Networking | Dating Websites | Membership Websites | Zend Framework | PRADO | Symfony | Wordpress

ASP.NET | PHP | C# | Objective-C | AJAX | Web 2.0 | HTML | CSS | Javascript | MySQL | MSSQL

 


Host unlimited domains + get up to 5 FREE domains with your 1&1 hosting account. Must see monthly hosting specials!


Invite your friends to Stable Flow

Enter up to 3 email addresses of your friends: