This page explains how to set up an example "Cookbook" database for use with RadRails.
Contents |
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:
- In Eclipse, switch to the SQL Explorer perspective.
- 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.
- 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.)
- In Eclipse, switch to the RadRails perspective.
- In the Ruby Explorer View, navigate to your cookbook project, and expand the config folder, and open the database.yml file in the Editor.
- In the database.yml file, update the database name to "cookbook" and update the password information (if necessary).
- 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:
- In Eclipse, switch to the SQL Explorer perspective.
- In the SQL Editor, select both your connection and your cookbook database from the drop-down lists at the top of the Editor.
- 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.
- 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:
- In Eclipse, switch to the RadRails perspective.
- Create a Recipe model:
- Click the Generators tab to go to the Generators View.
- From the drop-down box on the left, select model.
- Below that drop-down box, choose the Create option.
- In the drop-down box on the right, type Recipe.
- Click the Go button to create the Recipe model.
- Check the model information:
- In the Rails Navigator View, navigate to cookbook > app > models and open the recipe.rb file.
- The contents of the file should be the same as the code excerpt below:
class Recipe < ActiveRecord::Base end
- Create the controller:
- In the Generators View, select controller from the drop-down list on the left.
- Below that drop-down box, choose the Create option.
- In the drop-down box on the right, type Recipe.
- Click the Go button to create the Recipe controller.
- Add the controller information:
- In the Rails Navigator View, navigate to cookbook > app > controllers and open the recipe_controller.rb file.
- 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
- Save your changes.
- Check the "new recipe" page in your browser.
- 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.
- In the browser preview, navigate to http://localhost:[port]/recipe/new and press Return.
Your page should look similar to the example below:
- Add description and date columns to the recipes table:
- Refresh your browser preview. It should now look like the image below:
- 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:
- If you are not already in the RadRails perspective, switch to the RadRails perspective in Eclipse.
- In the Rails Navigator View, navigate to cookbook > app > controllers and open the recipe_controller.rb file for editing.
- 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
- Create a new file to manage listing for the database:
- In the Rails Navigator View, navigate to cookbook> app > views and right-click the recipe folder then select New > File from the context menu.
- Name the new file list.rhtml and click the Finish button to create the new file.
- 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>
- 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.
- Save your changes.
- Refresh your browser. Your page should look like the page below:
Adding Categories to the Cookbook
To add recipe categories to your Cookbook:
- In Eclipse, switch to the SQL Explorer perspective.
- Create the categories table.
- In your SQL editor, select the cookbook database from the drop-down list at the top of the editor.
- 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;
- Click the Execute button
to create the table.
- 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.
- Create a Category controller:
- In Eclipse, switch to the RadRails perspective.
- Click the Generators tab to go to the Generators View.
- In the Generators View, select controller from the drop-down list on the left.
- Below that drop-down box, choose the Create option.
- In the drop-down box on the right, type Category.
- Click the Go button to create the Category controller.
- Create a Category model:
- In Eclipse, switch to the RadRails perspective.
- Click the Generators tab to go to the Generators View.
- In the Generators View, select model from the drop-down list on the left.
- Below that drop-down box, choose the Create option.
- In the drop-down box on the right, type Category.
- Click the Go button to create the Category model.
- In the Rails Navigator View, navigate to cookbook > app > controllers and open the category_controller.rb file for editing.
- 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
- Save your changes.
- In your RadRails browser, go to the URL http://localhost:[port]/category/new. Your page should look similar to the one below:
- 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:
- In Eclipse, switch to the SQL Explorer perspective.
- Add the category_id field to the recipes table:
- 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;
- Click the Execute button
to update the table.
- In the Database Structures View, right-click the Cookbook database and select Refresh from the context menu to refresh the database.
- In the SQL Editor, select the Cookbook database from the drop-down list and copy and paste in the following code:
- In Eclipse, switch to the RadRails perspective.
- Update the recipe model:
- In the Rails Navigator View, navigate to cookbook > app > models and open the recipe.rb file for editing.
- 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
- Save your changes.
- In the models folder, open the category.rb file for editing.
- 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
- Save your changes.
- Edit the "edit recipe" action:
- In the Rails Navigator View, navigate to cookbook > app > controllers, and open the recipe_controller.rb file for editing.
- 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
- Save your changes.
- Create a new file for editing recipes:
- 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.
- 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>
- Save your changes.
- Edit a recipe:
- Go to your browser preview and go to the URL http://localhost:[port]/recipe/list.
- Click the link to one of your recipes, click the Edit link on the recipe page, and change the recipe category for that recipe.
- Repeat assigning a category to each recipe that you have in your database.
- Make category_id a required field for all recipes:
Displaying Categories in a list of all Recipes
To display categories in a list of all recipes:
- In Eclipse, switch to the RadRails perspective.
- In the Rails Navigator View, navigate to the cookbook > app > views > recipe folder, and open the list.rhtml file for editing.
- 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>
- 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.