RoR - Creating our first CRUD application (part 2)
In the first topic, we’ve learned how to create a Rails project, as well as setup it. Also we implemented a CREATE functionality using many different HTML components.
Now, we are going to implement the READ, or better, the list functionality. Also, we are going to learn how to use the render :partial helper class to let our code easier to maintain.
First of all, we have to figure out what is going to be shown in the list. Usually, we have a list containing the most important fields from the record. In our case, let’s use the fields name, address and sex.
Creating the list of the customers
It is a simple task. Firstly, let’s create a method called list_customers in the CustomersController.rb file. Also put a single command to get the entire list of customers from the database.
def list_customers
@customers = Customer.find(:all, :order => :name)
end
As you can see above, only one line is required to get ALL values from the database. Also the parameter :order tells to ActiveRecord to order the values through name field.
Next step is to create the list_customers.html.erb file. Create it into app/views/customers directory. The content of this file is described below. Pay attention on the code with a red circle, they are the Ruby code though.
- The first red circle is a simple IF to test if there are records in the @customers object.
- The second one is a bundle of command. A for to go through all values from the object and print them out to the HTML.
- Finally the last red circle is a else and a end command. If there is no record in the object, print out the “None customer found”.
After these changes, you can stop/start your server and try out the following link:
http://localhost:3000/customers/list_customers
If you have records into the database, a list of the customers should come up for you.
Remember: You can add new customer going to the following address:
http://localhost:3000/customers/new_customer
Creating the index
Although we have the LIST and NEW customer functionalities working, it is ugly because we have to type the URL to navigate through them. So let’s create a index.html.erb file (inside app/views/customers directory) and put a link to the new_customer and list_customer actions.
You do not need to create a method called index into the controller for time being, because none action is going to be active when the index.html.erb runs. So simply create the index.html.erb file and put the link them (using the link_to tag). An example below:

Take a look at the link_to helper method. It creates a link to the action you want to navigate for.
Using the render :partial method
So far we have a index that contains a link to new_customer and list_customers actions. However it would be better if the list of the customers would appears on the index page.
As we already have the list_customer actions implemented, we can use it inside the index page. This operation is called partial.
To create a partial is very simple. First of all, rename the list_customers.html.erb file to _list_customers.html.erb (look at the underscore in the begin of the file).
Now, we can remove the link_to that calls the list_customers action. We do not need it anymore.
Also, we can remove the method list_customers from CustomersController, but before do it, create a index method and put the content from the list_customers inside the index.

It has been done because the customers are going to be read from the database when the index page starts.
Finally let’s put the partial method into index.html.erb file. Add the following command:
<%= render :partial => "list_customers",:object => @customers %>
The :partial parameter tell to rails which file must be read. The :object parameter tells to rails if any object must be send to the partial, in our case, the @customers object created inside index method from Controller, must be send to the partial.
Running the index we’ve got the following result:

As you can see, our index page has the link to add new customer, as well as the list of the current customers.
We finished this topic. In the next one we are going to cover the DELETE functionality, as well as a little about AJAX.
You can download the program of this topic here.
I hope this topic be useful for anyone else.


Brazilian guy, IT Specialist, Linux User, IBM Certified SOA Fundamentals, Rational Developer, Sun Certified Java Associate 1.0, Sun Certified Java Programmer 1.4, Sun Certified Web Component Developer 1.4 and Sun Certified Business Component Developer 5. Also Ruby and Python enthusiastic.