RoR – Creating our first CRUD application (part 1)

February 27th, 2008  | Tags: , , , , ,

If you are looking for agility in web development, Ruby on Rails can be a good choice for you.Rails is a full-stack framework, not only a MVC framework. But, what does MVC mean? Basically MVC is a design pattern where the web system is broken in different layers. The M is the model layer, responsible for the business logic. V is the view, usually an HTML file and the C is the controller, a bridge between the M and V.

Who is coming from Java (like myself), certainly already heard about MVC framework, such as: Struts, JSF, Spring MVC. However Rails is not only a MVC framework. Rails is a full-stack framework. Besides the MVC, it provides the ActiveRecord framework which help us working on database layer. ActiveRecord is a ORM written in Ruby and it is used by Rails to insert, delete, update values from the database.

Rails also implement the Configuration over convention, it means, you do not need to configure many and many XMLs to map the controllers and entities.

If you are a Java programmer, you will think: “Hey, JBoss Seam is a full-stack framework as well. It uses JSF, EJB3/JPA and IoC. You cannot do anything else to integrate those technologies”. Yes, you are right. I do not want to tell you “Ruby is better than Java, Rails is better than any Java’s framework”. I want to show you how Rails is hot and how it can be useful to build web applications.

First of all, you have to install the Ruby and Rails. As Ruby runs on a Virtual Machine, the same file can be performed on Windows, Linux/Unix, MacOS operating system.

If you do not know how to install Ruby and Rails on Linux, check out the following links: Getting Started in Ruby and Installing Rails on Ubuntu 7.10.

Ruby is simple, however a good IDE make our life easier. Today we have two majors IDEs: Netbeans 6 and Eclipse + RadRails plugin. I prefer eclipse (my opinion), however in Linux I usually use the vim editor plus Vim-Ruby and Rails.Vim plugins. I advice you the check them out too :)

You must to install/use a database to work with Rails. MySQL probably is the most used in Rails community and can be a good choice for you. Here there is a topic showing how to install MySQL Database and Ruby support.

Creating our CRUD application

First of all, let’s create our Rails application. For our example, the application’s name is gonna be “RubyCRUD” and the database is gonna be the mysql. Type the following in the console:

rails RubyCRUD --database=mysql

After that, edit the file config/database.yml and put the database configuration in “development” section. In my case (using mysql), I’ve changed the database and password fields.

Creating the Model

Let’s get started our application by the model layer. However let’s gather the requirements first.

In our CRUD, let’s try to use different user interface components. Thus, we are going to create a CRUD of Customers. Then let’s create the customer model. Type the following command:

ruby script/generate model customer

After the model is created, edit the db/migrate/001_create_customers.rb file. Add the following columns into the file.

As you can see above, there will be 9 fields on the User Interface.

To make our life easier, you can run the generate script using the fields as parameters, for instance:

ruby script/generate model customer name:string address:string phone:string

Certainly the command above can save many time of your job.Run the Rake tool to create this table, as well as its fields into the database. Type the following:

rake db:migrate

If for any reason you want to rollback, type rake db:migrate VERSION=0

Creating the Controller and the Form Field

There will be a controller called customers. Also, there will be a method called new_customer. Our form is going to be created in a file called new_customer.html.erb.

To create the controller and the html file, type the following:

ruby script/generate controller customers new_customer

Before work on the new_customer.html.erb file, let’s create our default layout. To do that, create a file called customers.html.erb into the directory app/views/layouts/. The customers.html.erb looks like the code below:

If you are not familiar with Rails layout, check it out in a google search.

Now let’s create the form. Open the file new_customer.html.erb previously created. Insert the form code inside it, like the image below:

Although the form above does not have many fields, I tried to create a form using many different components. In the example above, we used 5 components: Text Field, Text Area, Radio Button, Select, Date Select.

Looking at the Select component, you can see that it is referencing to the Customer::JOBS_AVAILABLE constant. but it has not been created yet, isn’t it? Not yet, but it is simple, just add the following code to the customer.rb model file.

As you can see, the constant is a simple Array. The first value is the value displayed and the second value is the field value.

After those changes, let’s start the server, typing the following:

ruby script/server

And also open your favorite web browser and type the URL:

http://localhost:3000/customers/new_customer

A form screen, like below, should come up:


But do not click on Submit button yet. We must implement the method save_customer, as well as build the validation

Creating the validation

In rails applications, the validation is created inside the model layer. So open the customers.rb file (remember, a constant supposed to be created in this file) and add the validation. Below following an example:

Implementing the save_customer method

So far we have the model created, the table created, the form created and the validation created. Now let’s create the save_customer method (inside customers controller) .

This method is going to save the values from User Interface into the database. If successfully, the flash object will be created with a success message, otherwise an error will be displayed. Look at the code below:


Also, you have to change the layout to display the flash[:notice] object. Add the following lines to the customers.html.erb layout file.

After these changes, you will be able to test the ADD NEW CUSTOMER functionality. Fell free to make as much as tests you can.

Conclusion

In this topic, we talked about the first step to build a CRUD application using Rails 2. I showed how to build the Model layer, create the form using many components, how to make a validation and how to use the save method from ActiveRecord.

In the next one, we are going to talk about the SELECT, DELETE and UPDATE functionalities.

If you want, you can download the example HERE.

I hope this topic (and in the future the next one) be useful for anyone.

  1. orb_de_souza
    March 13th, 2008 at 21:24
    #1

    Excelente post,principalmente pra quem quer começar a entrar no mundo RoR!
    Agora,teria alguma referência para regras de negócio mais complexas ?

TOP