RoR - Creating our first CRUD application (part 4)
Finally we are getting started the last topic about our CRUD application.
In this topic we are going to talk about the UPDATE functionality. Also, we are going to review the entire code and improve it.
After this topic, you will be able to create yourself a CRUD application without scaffold generate.
Adding the Update link on List of the Customers
First of all, let’s add a link called update in the right side of the delete link on the list of the customers. So edit the file app/views/customers/_list_customers.html.erb and put the update link in the right side of the delete link. The new code looks like this:
<td>
<%= link_to_remote "Delete",:url => {:action => :delete_customer, :id => customer},
confirm => "Are you sure ?" %> /
<%= link_to "Update", :action => :update_customer, :id => customer %>
</td>
Look at the new link_to helper method. It calls the update_customer method from the controller. If you run the application, you will see the following:
Implementing the update_customer method
We need an update_customer method in the controller. This method has two functions.
- When the request comes from the list, load the current Customers to the @customer object
- When the request comes from the form (request.post?), it updates the new customer values
Meanwhile, let’s implement only the option 1 (request comes fom the list). The code looks like below:

Look at the code after the else. It loads the Customer and save the information into @customer object. This @customer is going to be used in the web page.
Creating the web page
Of course we could update all values from the database, however for this example we are going to allow only the name and address changing.
The update page looks like the add page, except by the fact the object into form_for must be the same then the object loaded into the update_customer method. In our case, the object’s name is: @customer. The app/views/customers/update_customer.html.erb file looks like below:

It is simple, isn’t it?
Implementing the second part of the update_customer method
Now let’s do the part of the update_customer method where the values are going to be updated in fact.
There are many ways to update a value from the database, however for this case, the better one is to use the update_attributes method from ActiveRecord.
Basically, we are going to use two lines:
- Load a new object (using find method) with the current id.
- Updated it using the update_attributes method. Its parameter could be the object customer.
Look at the method implemented:

Look at the red circle. It contains the code for the update function.
Try out the application and make some testing using the new update functionality.
Our application has finished. Now you are able to create a CRUD yourself without the scaffold generator.
Improving the code
Although our application supposing is working, there are some details we did not cover. For instance: What will happen if the user type the update URL directly in the browser, but using a invalid id? For example: http://localhost:3000/customers/update_customer/1000 and the id 1000 does not exist yet? We will get an error screen:

It happens because ActiveRecord tried to load a ID from the database but this ID does not exist. How could we protect it?
A simple, and good approach, is to create the exception handling. Let’s use the begin, rescue commands. In this case, we are going to test if the value is found, if not, redirect to index page and show an error message (better than before).
Adding exception handling
As mentioned before, let’s add an exception handling, when the code tries to load the id.

And the result is going to be:

Have you seen how easy is work with exception handling?
When you will be working in a real application, make sure to handle exception in the places that you think you can get an error. Investigate the rest of the code and try to figure out other places where a handle would be good.
Logging support
Another good approach (actually a required approach for any application) is the logging support. Fortunately the log support in Rails is pretty easy.
Let’s use the same example above and log the error if it happens. Add the following after the rescue:
logger.error("Attempt to access invalid customer #{params[:id]} in Update functionality")
You can see the log events on the rails_project/log directory.
Adding CSS stylesheet
I’ve already seen in some Rails’ forum the people asking for CSS stylesheet. Actually, who is getting started in rails, usually has doubt where to put the css files, as well as add it to the HTML file.
The css files must be placed into rails_projeto/public/stylesheets directory.
To add it to the HTML file, you can use the stylesheet_link_tag method. Example:
<%= stylesheet_link_tag "layout" %>
We’ve finished
We’ve finished our tutorial (broken in 4 topics). If you are familiar with scaffold and would like to create yourself a CRUD application, I think these topics may be useful.


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.