Powerful Google developer tools for immediate impact! (2023-24 C)
Born_ruby_on_rails
1. Efficiently Creating Database Applications with Ruby On Rails
By: Kenton Born
Abstract
This tutorial was created as a simplified, shorter version of the tutorial provided by
InstantRails (“http://instantrails.rubyforge.org/wiki/wiki.pl”). The tutorial shows how a
user can efficiently set up an application that allows for the adding, editing, reading, and
deleting of database items.
Procedure
• If you have not done so, install InstantRails at
http://instantrails.rubyforge.org/wiki/wiki.pl. Follow their instructions for
installing and configuring MySQL to work with InstantRails.
• Open the command prompt and navigate to ../ruby/InstantRails/rails_apps
• Type “rails mycookbook”
o This created the MVC file architecture for the application
2. • By default, it wants to use a database called “mycookbook_development”, so we
want to create this database.
o The default database be changed by editing the
mycookbookconfigdatabase.yml file
o Enter the following commands into the command prompt
mysql -u root -p
create database cookbook2_development;
exit
• Create a script at ..cookbook2dbcreate.sql to create tables for the database
It should contain the following sql:
---------------------------------------------------
drop table if exists recipes;
drop table if exists categories;
create table categories (
id int not null auto_increment,
name varchar(100) not null default '',
primary key(id)
) engine=InnoDB;
create table recipes (
id int not null auto_increment,
category_id int not null,
title varchar(100) not null default '',
description varchar(255) null,
date date null,
instructions text null,
constraint fk_recipes_categories foreign key (category_id) references categories(id),
primary key(id)
) engine=InnoDB;
-----------------------------------------------------
3. • Run the script
o Navigate back to the mycookbook directy
o Type: “mysql –u root –p mycookbook_development <dbcreate.sql”
If there is no output, it completed successfully.
• Now, we want to generate code based on our tables.
o Nagivaget to the mycookbook directory
o Type: “ruby scriptgenerate scaffold recipe recipe
This generates the model, view, and controller for the recipe table
The model is placed in mycookbookappmodelsrecipe.rb
The controller is placed in ..appcontrollersrecipe_controller.rb
Code was generated for creating, reading, updatings, and deleting
• Repeat the above step for the category table
o Type: “ruby scriptgenerate scaffold category category”
• Now let’s see what we have by starting up a server to run our application
o Navigate to the “mycookbook” directory
o Type: “mongrel_rails start”
• This tells the mongrel_server to run our application
• Go to: http://localhost:3000/category
4. • Click on “New category”, and you see the following:
• Go ahead and add a few categories such as “Breakfast” and “Salad”
• Now go to: http://localhost:3000/recipe
• Click on “New recipe”
5. • Try adding a recipe
o It won’t work, complaining that there is no category.
o The database knows about the connection, but the application does not
• We fix this by modifying the two table files in the model
o Add “has_many :recipes” to mycookbookappmodelscategory.rb
o Add “belongs_to :category” to mycookbookappmodelsrecipe.rb
6. • Finally, we must modify the view to allow us to select one of the available
categories
o Navigate to: mycookbookappviewsrecipe_form.rhtml
o Add the following code after the “Title” text field
<p><label for="recipe_category_id">Category</label><br/>
<%= select("recipe", "category_id", Category.find(:all).collect {|c| [c.name, c.id] }) %></p>
7. • Once again, navigate back to the “mycookbook” directory, and run the server
o Type: “mongrel_rails start”
o Go to: http://localhost:3000/recipe
• This time, select one of the available categories while inputting information.
8. • Once created, a view should come up showing all the added recipes so far.
9. • You are done creating the simple Ruby on Rails application!