Web Application Development Practical: Ruby on Rails - Working with Database

In this practical, you will learn how to integrating databases into the cookbook application.

Part I: Getting Up and Running Quickly with Scaffolding

Rails scaffolding is a quick way to generate some of the major pieces of an application. If you want to create the models, views, and controllers for a new resource in a single operation, scaffolding is the tool for the job.

Step 1) Creating a Resource

If you haven't created the cookbook project, or lost your code, you should first create a new project called cookbook as in previous practical:

rails new cookbook

If you have your cookbook project generated before, make sure you are in this folder after you fire up the terminal.

Now we can start by generating a scaffolded Recipe resource: this will represent a single recipe posting. Open a terminal and enter this command:

rails generate scaffold Recipe  title:string description:string date:date instructions:text

See what files have been generated, and in particular pay attention to the following files:

app/models/recipe.rb

app/controllers/recipes_controller.rb

app/views/recipes/*

Also the following database related files:

db/migrate/2012XXXXXXX_create_recipes.rb (XXXX stands for a series of number)

db/schema.rb

Open these files and try to understand them.

Step 2) Running a Migration

One of the products of the rails generate scaffold command is a database migration. Migrations are Ruby classes that are designed to make it simple to create and modify database tables. Rails uses rake commands to run migrations, and it’s possible to undo a migration after it’s been applied to your database. At this point, you can use a rake command to run the migration:

rake db:migrate

This command line of code will bring the database table to life. It defines actions for all CRUD operations, immediately allowing us to create, read, update, and delete recipes in our database!

Step 3) The Excitement Begins


Open a new Command Prompt with Ruby window, go to your cookbook folder and enter the following command:

rails server

Open a browser and navigate to http://127.0.0.1:3000/recipes/new. You should see something like Figure 1.



Figure 1. Creating a new recipe page


Now this is pretty cool! We haven't done much of anything and we can already start to populate our database.
OK, calm down and enter a test recipe. Fill in the fields as shown in Figure 2 and click on the Create button.



Figure 2. A new recipe


You should see the results, as in Figure 3.



Figure 3. The result's screen

Figure 4. A listing of all recipes

Then, click on "Back" you will see the list of recipes in your database (Figure 4). Next, try to add another one by clicking the "New recipe" link and entering the data, as in Figure 5.



Figure 5. Another new recipe


After you click Create you should see something like Figure 6.



Figure 6. A fuller list of all recipes


We now have an amazing amount of functionality, by merely building a database table and typing in a single line of code. It may not be pretty yet, but we'll fix that soon enough.

Part II: Searching the database

In this part we will learn how to search the database if a user types in a keyword.

Step 4) Search recipe on the database

Edit the "application.rb" file under cookbook\app\controllers\ and comment out the following line: "protect_from_forgery".

Edit the " index.html.erb" file under cookbook\app\views\recipes\ and add following code at the end of file.

<h3>Enter keyword</h3>
<form action ="recipes/search" method="post">
<input name = "key" type="input" />
<input value="Send" type="submit"/>
</form>

Edit the "recipes_controller.rb" file under cookbook\app\controllers\ and create the following method.

def search
@recipes = Recipe.find(:all, :conditions => ["title LIKE ?", "%#{params[:key]}%"])
end

In the directory cookbook\app\views\recipe, create a file named search.html.erb containing the following:

<html>
<head>
<title>Search results
</title>
</head>
<body>
<h1>Search results</h1>
<table border="1">
<tr>
<td width="40%"><p align="center"><i><b>Recipe</b></i></td>
<td width="20%"><p align="center"><i><b>Date</b></i></td>
</tr>
<% @recipes.each do |recipe| %>
<tr>
<td>
<a href="
<%=recipe.id%>
">
<%=recipe.title%>
</a>
</td>
<td><%= recipe.date %></td>
</tr>
<% end %>
</table>
<p> <a href="/recipes"> Back</a> </body>
</html>

Refresh your browser and try to use the search function that we have created then observe the result. You will see the below routing error as we haven't update the routing file yet.



Figure 7. Routing Error

Open config/routes.rb and modify the file as follows:



Figure 8. Edit the Routing file

If you don't understand the code in config/routes.rb, go to this link to check further details about Rail Routing (especially Section 2.9).

Now try to use the search function again. In the meantime, play around with adding, deleting, and editing recipes. Go ahead.

Part III

You are required to develop a simple web application called "To-do List" and its database.

  1. The database should contain several fields such as id, date, subject and detail.

  2. You are expected to add some records about the To-do List into your database.

  3. Add a search box in your web application, allowing users to search todo items based on the subject field.

  4. When the application is initialised, the database should contain the following records:

    • 10am, 10th, Mar 2012, WAD Helpdesk on 10th, Mar 2012, Question answering section of WAD.

    • 9am, 17th, Mar 2012, WAD Helpdesk on 17th, Mar 2012, Question answering section of WAD.