DOC PREVIEW
K-State CIS 764 - Efficiently Creating Database Applications with Ruby On Rails

This preview shows page 1-2-3 out of 9 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 9 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 9 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 9 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 9 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

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• 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 mycookbook\config\database.yml file o Enter the following commands into the command prompt  mysql -u root -p  create database cookbook2_development;  exit • Create a script at ..\cookbook2\db\create.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; -----------------------------------------------------• Run the script o Navigate back to the mycookbook directy o Type: “mysql –u root –p mycookbook_development <db\create.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 script\generate scaffold recipe recipe  This generates the model, view, and controller for the recipe table  The model is placed in mycookbook\app\models\recipe.rb  The controller is placed in ..\app\controllers\recipe_controller.rb  Code was generated for creating, reading, updatings, and deleting • Repeat the above step for the category table o Type: “ruby script\generate 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• 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”• 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 mycookbook\app\models\category.rb o Add “belongs_to :category” to mycookbook\app\models\recipe.rb• Finally, we must modify the view to allow us to select one of the available categories o Navigate to: mycookbook\app\views\recipe\_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>• 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.• Once created, a view should come up showing all the added recipes so far.• You are done creating the simple Ruby on Rails


View Full Document

K-State CIS 764 - Efficiently Creating Database Applications with Ruby On Rails

Documents in this Course
Load more
Download Efficiently Creating Database Applications with Ruby On Rails
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Efficiently Creating Database Applications with Ruby On Rails and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Efficiently Creating Database Applications with Ruby On Rails 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?