# Setting up the Cookbook database in RadRails [edit]

This page explains how to set up an example "Cookbook" database for use with RadRails.

## Introduction

This Help topic explains how to set up a sample database based on a "Cookbook" in RadRails. For more detailed information about performing various development tasks in RadRails, see the individual Help topics on the main RadRails wiki page:

http://aptana.com/docs/index.php/RadRails

Note: This Help topic is based on the Rolling with Ruby on Rails tutorial from ONLamp:

http://www.onlamp.com/pub/a/onlamp/2005/01/20/rails.html?page=1

## Instructions

This section will teach you how to set up a sample "Cookbook" database in RadRails.

### Creating the Cookbook database

You will need to follow the steps in Configuring the Eclipse SQL Explorer plug-in for RadRails and Getting started rolling with Ruby on Rails in RadRails before you will be able to create and populate the Cookbook database.

To create the Cookbook database:

1. In Eclipse, switch to the SQL Explorer perspective.
2. In the Connections View, right-click your connection and select Connect from the context menu to connect to your MySQL server.

   The Database Structure View displays all databases that are accessible through your connection.

3. In the SQL Editor, copy and paste the following code, then click the Execute button to create the database.

       CREATE DATABASE cookbook;

   SQL Explorer creates the "cookbook" database. You may need to re-start your workbench and re-connect to see the cookbook database displayed in your Database Structures View.(See image below.)

4. In Eclipse, switch to the RadRails perspective.
5. In the Ruby Explorer View, navigate to your cookbook project, and expand the config folder, and open the database.yml file in the Editor.
6. In the database.yml file, update the database name to "cookbook" and update the password information (if necessary).
7. Save your changes to the database.yml file.

You can now add tables to your cookbook database.

### Creating the Recipes table

To create a recipes table for your cookbook database:

1. In Eclipse, switch to the SQL Explorer perspective.
2. In the SQL Editor, select both your connection and your cookbook database from the drop-down lists at the top of the Editor.
3. In the SQL Editor, copy and paste the following text to create the recipes table:

       CREATE TABLE `recipes` (

         `id` int(11) NOT NULL AUTO_INCREMENT,

         `title` varchar(255) NOT NULL,

         `instructions` text NOT NULL,

         PRIMARY KEY (`id`)

       ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

   See image below for example.

4. Click the Execute button to create the table.

SQL Explorer creates the recipes table for you. You may have to refresh (right-click > Refresh) the cookbook database in the Database Structure View to see your changes.

### Creating the model

To create a model for a recipe entry:

1. In Eclipse, switch to the RadRails perspective.
2. Create a Recipe model:
   1. Click the Generators tab to go to the Generators View.
   2. From the drop-down box on the left, select model.
   3. Below that drop-down box, choose the Create option.
   4. In the drop-down box on the right, type Recipe.
   5. Click the Go button to create the Recipe model.
3. Check the model information:
   1. In the Rails Navigator View, navigate to cookbook > app > models and open the recipe.rb file.
   2. The contents of the file should be the same as the code excerpt below:

          class Recipe < ActiveRecord::Base
          end

4. Create the controller:
   1. In the Generators View, select controller from the drop-down list on the left.
   2. Below that drop-down box, choose the Create option.
   3. In the drop-down box on the right, type Recipe.
   4. Click the Go button to create the Recipe controller.
5. Add the controller information:
   1. In the Rails Navigator View, navigate to cookbook > app > controllers and open the recipe_controller.rb file.
   2. After the first line in the file, press Enter to create a new line and add the following code:

          scaffold :recipe

      The contents of your recipe_controller.rb file should be the same as the sample below:

          class RecipeController < ApplicationController
            scaffold :recipe
          end

   3. Save your changes.
6. Check the "new recipe" page in your browser.
   1. If your browser is not running, go to the Servers View and right-click the CookbookServer then select Start from the context menu. Once the server is running, right-click CookbookServer again, and select Launch Browser from the context menu.
   2. In the browser preview, navigate to http://localhost:[port]/recipe/new and press Return.

      Your page should look similar to the example below:

7. Add description and date columns to the recipes table:
   1. Copy and paste the following code into your SQL Editor:

          alter table `recipes`

           add column `description` varchar(255) DEFAULT NULL,

           add column `date` date DEFAULT NULL;

   2. Click the Execute button to add the columns to the recipes table.
8. Refresh your browser preview. It should now look like the image below:

9. Test your database by entering data for a sample recipe and click the Create button to create a recipe. You should see something similar to the "example" recipe below.

### Creating Actions and Views

To create actions and Views for your Cookbook database:

1. If you are not already in the RadRails perspective, switch to the RadRails perspective in Eclipse.
2. In the Rails Navigator View, navigate to cookbook > app > controllers and open the recipe_controller.rb file for editing.
3. Add a list definition to the recipe_controller.rb file. The contents of your file should be the same as the code excerpt below:

       class RecipeController < ApplicationController
         scaffold :recipe

         def list
         end
       end

4. Create a new file to manage listing for the database:
   1. In the Rails Navigator View, navigate to cookbook> app > views and right-click the recipe folder then select New > File from the context menu.
   2. Name the new file list.rhtml and click the Finish button to create the new file.
   3. Paste the following code into your list.rhtml file and save your changes:

          <html>

          <head>

          <title>All Recipes</title>

          </head>

          <body>

          <h1>Online Cookbook - All Recipes</h1>

          <table border="1">

          <tr>

          <td width="80%"><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><%= link_to recipe.title, :action => "show", :id => recipe.id %></td>

          <td><%= recipe.date %></td>

          </tr>

          <% end %>

          </table>

          <p><%= link_to "Create new recipe", :action => "new" %></p>

          </body>

          </html>

5. Open your recipe_controller.rb file again, and add the line `@recipes=Recipe.find_all` to the list definition, as shown below:

       class RecipeController < ApplicationController
         scaffold :recipe

         def list
           @recipes = Recipe.find(:all)
         end
       end

   When called, this lists all of the recipes in the database.

6. Save your changes.
7. Refresh your browser. Your page should look like the page below:

### Adding Categories to the Cookbook

To add recipe categories to your Cookbook:

1. In Eclipse, switch to the SQL Explorer perspective.
2. Create the categories table.
   1. In your SQL editor, select the cookbook database from the drop-down list at the top of the editor.
   2. Copy and paste the following code into the SQL editor:

          CREATE TABLE `categories` (

            `id` int(11) NOT NULL AUTO_INCREMENT,

            `name` varchar(50) NOT NULL,

            PRIMARY KEY (`id`)

          ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

   3. Click the Execute button to create the table.
3. Refresh the database by right-clicking your database in the Database Structures View and selecting Refresh from the context menu.

   If you expand the Tables node, you will now see the Categories table.

4. Create a Category controller:
   1. In Eclipse, switch to the RadRails perspective.
   2. Click the Generators tab to go to the Generators View.
   3. In the Generators View, select controller from the drop-down list on the left.
   4. Below that drop-down box, choose the Create option.
   5. In the drop-down box on the right, type Category.
   6. Click the Go button to create the Category controller.
5. Create a Category model:
   1. In Eclipse, switch to the RadRails perspective.
   2. Click the Generators tab to go to the Generators View.
   3. In the Generators View, select model from the drop-down list on the left.
   4. Below that drop-down box, choose the Create option.
   5. In the drop-down box on the right, type Category.
   6. Click the Go button to create the Category model.
6. In the Rails Navigator View, navigate to cookbook > app > controllers and open the category_controller.rb file for editing.
7. In the line below the class declaration, add the code `scaffold :category` so that your code looks like the example below:

       class CategoryController < ApplicationController
         scaffold :category
       end

8. Save your changes.
9. In your RadRails browser, go to the URL http://localhost:[port]/category/new. Your page should look similar to the one below:

10. Use the New Category page in your browser to create a "Snacks" and a "Beverages" category. Once you use the form to create each of these categories, your page should look like the image below:

### Assigning a Category to each Recipe

To assign a category to each recipe in the cookbook database:

1. In Eclipse, switch to the SQL Explorer perspective.
2. Add the category_id field to the recipes table:
   1. In the SQL Editor, select the Cookbook database from the drop-down list and copy and paste in the following code:

          alter table `recipes`

           add column `category_id` int(11) DEFAULT NULL;

   2. Click the Execute button to update the table.
   3. In the Database Structures View, right-click the Cookbook database and select Refresh from the context menu to refresh the database.
3. In Eclipse, switch to the RadRails perspective.
4. Update the recipe model:
   1. In the Rails Navigator View, navigate to cookbook > app > models and open the recipe.rb file for editing.
   2. Add the code `belongs_to :category` to the recipe.rb file so that your code looks like the example below:

          class Recipe < ActiveRecord::Base
            belongs_to :category
          end

   3. Save your changes.
   4. In the models folder, open the category.rb file for editing.
   5. Add the code `has_many :recipes` to your file so that it looks like the example below:

          class Category < ActiveRecord::Base
            has_many :recipes
          end

   6. Save your changes.
5. Edit the "edit recipe" action:
   1. In the Rails Navigator View, navigate to cookbook > app > controllers, and open the recipe_controller.rb file for editing.
   2. Copy and paste the edit definition from the code sample below into your recipe_controller.rb file:

          class RecipeController < ApplicationController
            scaffold :recipe

            def list
              @recipes = Recipe.find(:all)
            end

            def edit
              @recipe = Recipe.find(@params["id"])
              @categories = Category.find(:all)
            end
          end

   3. Save your changes.
6. Create a new file for editing recipes:
   1. In the Rails Navigator View, navigate to the cookbook > app > views > recipe folder, right-click the folder and create a new file called edit.rhtml.
   2. Copy and paste the following HTML code into the edit.rhtml file:

          <html>

           <head>

            <title>Edit Recipe</title>

           </head>

           <body>

           <h1>Edit Recipe</h1>

           <form action="../update/<%= @recipe.id %>" method="POST"">

            <input id="recipe_id" name="recipe[id]" size="30"

                   type="hidden" value="<%= @recipe.id %>" />

            <p><b>Title</b><br>

            <input id="recipe_title" name="recipe[title]" size="30"

                   type="text" value="<%= @recipe.title %>" />

            </p>

            <p><b>Description</b><br>

            <input id="recipe_description" name="recipe[description]"

                   size="30" type="text"

                   value="<%= @recipe.description %>" />

            </p>

            <p><b>Category:</b><br>

            <select name="recipe[category_id]">

             <% @categories.each do |category| %>

                 <option value="<%= category.id %>"

                   <%= ' selected' if category.id == @recipe.category_id %>>

                   <%= category.name %>

                 </option>

             <% end %>

            </select></p>

            <p><b>Instructions</b><br>

            <textarea cols="40" id="recipe_instructions"

                      name="recipe[instructions]"

                      rows="20" wrap="virtual">

              <%= @recipe.instructions %>

            </textarea> </p>

            <input type="submit" value="Update" />

           </form>

           <a href="/recipe/show/<%= @recipe.id %>">

             Show

           </a> |

            <a href="/recipe/list">

            Back

           </a>

           </body>

          </html>

   3. Save your changes.
7. Edit a recipe:
   1. Go to your browser preview and go to the URL http://localhost:[port]/recipe/list.
   2. Click the link to one of your recipes, click the Edit link on the recipe page, and change the recipe category for that recipe.
   3. Repeat assigning a category to each recipe that you have in your database.
8. Make category_id a required field for all recipes:
   1. Switch to the SQL Explorer perspective.
   2. Copy and paste the following code into the SQL Editor:

          alter table `recipes`

           modify `category_id` int(11) NOT NULL AFTER `id`;

   3. Select the Cookbook database in the SQL Editor, and click the Execute button to update the table.

### Displaying Categories in a list of all Recipes

To display categories in a list of all recipes:

1. In Eclipse, switch to the RadRails perspective.
2. In the Rails Navigator View, navigate to the cookbook > app > views > recipe folder, and open the list.rhtml file for editing.
3. Copy and paste the code sample below, and use it to replace the current code in the list.rhtml file.

       <html>

       <head>

       <title>All Recipes</title>

       </head>

       <body>

       <h1>Online Cookbook - All Recipes</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>Category</b></i></td>

        <td width="20%"><p align="center"><i><b>Date</b></i></td>

        </tr>

        <% @recipes.each do |recipe| %>

        <tr>

        <td><%= link_to recipe.title, :action => "show", :id => recipe.id %></td>

        <td><%= recipe.category.name %></td>

        <td><%= recipe.date %></td>

        </tr>

        <% end %>

       </table>

       <p><%= link_to "Create new recipe", :action => "new" %></p>

       </body>

       </html>

4. Go to the URL http://localhost:[port]/recipe/list in your browser, and you should see that the Category field has been added to each recipe.

## Related Topics

- Rolling with Ruby on Rails in RadRails
- Getting started rolling with Ruby on Rails in RadRails

Retrieved from "http://aptana.com/docs/index.php/Setting_up_the_Cookbook_database_in_RadRails"

Categories: AptanaHelp | RadRails