Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide


  1. 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 (“”). 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 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. 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. 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. 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. 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. 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| [,] }) %></p>
  7. 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. 8. • Once created, a view should come up showing all the added recipes so far.
  9. 9. • You are done creating the simple Ruby on Rails application!