SlideShare uma empresa Scribd logo
1 de 9
Baixar para ler offline
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
             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;

-----------------------------------------------------
•   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
•   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 mycookbookappmodelscategory.rb
      o Add “belongs_to :category” to mycookbookappmodelsrecipe.rb
•   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>
•   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 application!

Mais conteúdo relacionado

Mais procurados

Drupal 7 advanced ajax
Drupal 7 advanced ajaxDrupal 7 advanced ajax
Drupal 7 advanced ajax
merlinofchaos
 
Jqeury ajax plugins
Jqeury ajax pluginsJqeury ajax plugins
Jqeury ajax plugins
Inbal Geffen
 

Mais procurados (19)

Drupal 7 advanced ajax
Drupal 7 advanced ajaxDrupal 7 advanced ajax
Drupal 7 advanced ajax
 
Jqeury ajax plugins
Jqeury ajax pluginsJqeury ajax plugins
Jqeury ajax plugins
 
RoR 101: Session 6
RoR 101: Session 6RoR 101: Session 6
RoR 101: Session 6
 
Mule flow complete
Mule flow completeMule flow complete
Mule flow complete
 
Htaccess
HtaccessHtaccess
Htaccess
 
Webapps without the web
Webapps without the webWebapps without the web
Webapps without the web
 
RoR 101: Session 2
RoR 101: Session 2RoR 101: Session 2
RoR 101: Session 2
 
WordCamp Raleigh 2015 - So You Want to Build and Release a Plugin
WordCamp Raleigh 2015 - So You Want to Build and Release a PluginWordCamp Raleigh 2015 - So You Want to Build and Release a Plugin
WordCamp Raleigh 2015 - So You Want to Build and Release a Plugin
 
Simple acl with laravel
Simple acl with laravelSimple acl with laravel
Simple acl with laravel
 
RoR 101: Session 6
RoR 101: Session 6RoR 101: Session 6
RoR 101: Session 6
 
前后端mvc经验 - webrebuild 2011 session
前后端mvc经验 - webrebuild 2011 session前后端mvc经验 - webrebuild 2011 session
前后端mvc经验 - webrebuild 2011 session
 
Cocoa on-rails-3rd
Cocoa on-rails-3rdCocoa on-rails-3rd
Cocoa on-rails-3rd
 
RoR 101: Session 5
RoR 101: Session 5RoR 101: Session 5
RoR 101: Session 5
 
Mock component in munit
Mock component in munitMock component in munit
Mock component in munit
 
Alpine.js: the outsider Javascript framework
Alpine.js: the outsider Javascript frameworkAlpine.js: the outsider Javascript framework
Alpine.js: the outsider Javascript framework
 
Jms selector
Jms selectorJms selector
Jms selector
 
RoR 101: Session 3
RoR 101: Session 3RoR 101: Session 3
RoR 101: Session 3
 
Javascript and jQuery intro
Javascript and jQuery introJavascript and jQuery intro
Javascript and jQuery intro
 
Orchestration
OrchestrationOrchestration
Orchestration
 

Destaque

Scalable vertical search engine with hadoop
Scalable vertical search engine with hadoopScalable vertical search engine with hadoop
Scalable vertical search engine with hadoop
datasalt
 

Destaque (8)

Addressing scalability challenges in peer-to-peer search
Addressing scalability challenges in peer-to-peer searchAddressing scalability challenges in peer-to-peer search
Addressing scalability challenges in peer-to-peer search
 
Scalability
ScalabilityScalability
Scalability
 
Challenges in Scaling & Globalization of a Web Application - The Slideshare E...
Challenges in Scaling & Globalization of a Web Application - The Slideshare E...Challenges in Scaling & Globalization of a Web Application - The Slideshare E...
Challenges in Scaling & Globalization of a Web Application - The Slideshare E...
 
Top 10 Scalability Mistakes
Top 10 Scalability MistakesTop 10 Scalability Mistakes
Top 10 Scalability Mistakes
 
Scalability At Kueski
Scalability At KueskiScalability At Kueski
Scalability At Kueski
 
Scalable vertical search engine with hadoop
Scalable vertical search engine with hadoopScalable vertical search engine with hadoop
Scalable vertical search engine with hadoop
 
Things you should know about Scalability!
Things you should know about Scalability!Things you should know about Scalability!
Things you should know about Scalability!
 
Hadoop Real Life Use Case & MapReduce Details
Hadoop Real Life Use Case & MapReduce DetailsHadoop Real Life Use Case & MapReduce Details
Hadoop Real Life Use Case & MapReduce Details
 

Semelhante a Born_ruby_on_rails

Staging Drupal 8 31 09 1 3
Staging Drupal 8 31 09 1 3Staging Drupal 8 31 09 1 3
Staging Drupal 8 31 09 1 3
Drupalcon Paris
 
td_mxc_rubyrails_shin
td_mxc_rubyrails_shintd_mxc_rubyrails_shin
td_mxc_rubyrails_shin
tutorialsruby
 
td_mxc_rubyrails_shin
td_mxc_rubyrails_shintd_mxc_rubyrails_shin
td_mxc_rubyrails_shin
tutorialsruby
 
MySQL Tips for WordPress
MySQL Tips for WordPressMySQL Tips for WordPress
MySQL Tips for WordPress
dsero
 

Semelhante a Born_ruby_on_rails (20)

Rapid Application Development with CakePHP 1.3
Rapid Application Development with CakePHP 1.3Rapid Application Development with CakePHP 1.3
Rapid Application Development with CakePHP 1.3
 
Staging Drupal 8 31 09 1 3
Staging Drupal 8 31 09 1 3Staging Drupal 8 31 09 1 3
Staging Drupal 8 31 09 1 3
 
Extracting Plugins And Gems From Rails Apps
Extracting Plugins And Gems From Rails AppsExtracting Plugins And Gems From Rails Apps
Extracting Plugins And Gems From Rails Apps
 
Open erp technical_memento_v0.6.3_a4
Open erp technical_memento_v0.6.3_a4Open erp technical_memento_v0.6.3_a4
Open erp technical_memento_v0.6.3_a4
 
td_mxc_rubyrails_shin
td_mxc_rubyrails_shintd_mxc_rubyrails_shin
td_mxc_rubyrails_shin
 
td_mxc_rubyrails_shin
td_mxc_rubyrails_shintd_mxc_rubyrails_shin
td_mxc_rubyrails_shin
 
Grails Connecting to MySQL
Grails Connecting to MySQLGrails Connecting to MySQL
Grails Connecting to MySQL
 
Grails patterns and practices
Grails patterns and practicesGrails patterns and practices
Grails patterns and practices
 
Chef Fundamentals Training Series Module 2: Workstation Setup
Chef Fundamentals Training Series Module 2: Workstation SetupChef Fundamentals Training Series Module 2: Workstation Setup
Chef Fundamentals Training Series Module 2: Workstation Setup
 
Digital Ocean Presentation - Ruby Dev Stackup - The Flatiron School
Digital Ocean Presentation - Ruby Dev Stackup - The Flatiron School Digital Ocean Presentation - Ruby Dev Stackup - The Flatiron School
Digital Ocean Presentation - Ruby Dev Stackup - The Flatiron School
 
Bye bye $GLOBALS['TYPO3_DB']
Bye bye $GLOBALS['TYPO3_DB']Bye bye $GLOBALS['TYPO3_DB']
Bye bye $GLOBALS['TYPO3_DB']
 
My first powershell script
My first powershell scriptMy first powershell script
My first powershell script
 
Chef advance
Chef advanceChef advance
Chef advance
 
Chef advance
Chef advanceChef advance
Chef advance
 
Evolutionary Database Design
Evolutionary Database DesignEvolutionary Database Design
Evolutionary Database Design
 
Codegnitorppt
CodegnitorpptCodegnitorppt
Codegnitorppt
 
MySQL on Docker and Kubernetes
MySQL on Docker and KubernetesMySQL on Docker and Kubernetes
MySQL on Docker and Kubernetes
 
Codeinator
CodeinatorCodeinator
Codeinator
 
MySQL Tips for WordPress
MySQL Tips for WordPressMySQL Tips for WordPress
MySQL Tips for WordPress
 
Show Some Spine!
Show Some Spine!Show Some Spine!
Show Some Spine!
 

Mais de tutorialsruby

&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />
tutorialsruby
 
TopStyle Help &amp; &lt;b>Tutorial&lt;/b>
TopStyle Help &amp; &lt;b>Tutorial&lt;/b>TopStyle Help &amp; &lt;b>Tutorial&lt;/b>
TopStyle Help &amp; &lt;b>Tutorial&lt;/b>
tutorialsruby
 
The Art Institute of Atlanta IMD 210 Fundamentals of Scripting &lt;b>...&lt;/b>
The Art Institute of Atlanta IMD 210 Fundamentals of Scripting &lt;b>...&lt;/b>The Art Institute of Atlanta IMD 210 Fundamentals of Scripting &lt;b>...&lt;/b>
The Art Institute of Atlanta IMD 210 Fundamentals of Scripting &lt;b>...&lt;/b>
tutorialsruby
 
&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />
tutorialsruby
 
&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />
tutorialsruby
 
Standardization and Knowledge Transfer – INS0
Standardization and Knowledge Transfer – INS0Standardization and Knowledge Transfer – INS0
Standardization and Knowledge Transfer – INS0
tutorialsruby
 
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa0602690047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
tutorialsruby
 
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa0602690047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
tutorialsruby
 
BloggingWithStyle_2008
BloggingWithStyle_2008BloggingWithStyle_2008
BloggingWithStyle_2008
tutorialsruby
 
BloggingWithStyle_2008
BloggingWithStyle_2008BloggingWithStyle_2008
BloggingWithStyle_2008
tutorialsruby
 
cascadingstylesheets
cascadingstylesheetscascadingstylesheets
cascadingstylesheets
tutorialsruby
 
cascadingstylesheets
cascadingstylesheetscascadingstylesheets
cascadingstylesheets
tutorialsruby
 

Mais de tutorialsruby (20)

&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />
 
TopStyle Help &amp; &lt;b>Tutorial&lt;/b>
TopStyle Help &amp; &lt;b>Tutorial&lt;/b>TopStyle Help &amp; &lt;b>Tutorial&lt;/b>
TopStyle Help &amp; &lt;b>Tutorial&lt;/b>
 
The Art Institute of Atlanta IMD 210 Fundamentals of Scripting &lt;b>...&lt;/b>
The Art Institute of Atlanta IMD 210 Fundamentals of Scripting &lt;b>...&lt;/b>The Art Institute of Atlanta IMD 210 Fundamentals of Scripting &lt;b>...&lt;/b>
The Art Institute of Atlanta IMD 210 Fundamentals of Scripting &lt;b>...&lt;/b>
 
&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />
 
&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />
 
Standardization and Knowledge Transfer – INS0
Standardization and Knowledge Transfer – INS0Standardization and Knowledge Transfer – INS0
Standardization and Knowledge Transfer – INS0
 
xhtml_basics
xhtml_basicsxhtml_basics
xhtml_basics
 
xhtml_basics
xhtml_basicsxhtml_basics
xhtml_basics
 
xhtml-documentation
xhtml-documentationxhtml-documentation
xhtml-documentation
 
xhtml-documentation
xhtml-documentationxhtml-documentation
xhtml-documentation
 
CSS
CSSCSS
CSS
 
CSS
CSSCSS
CSS
 
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa0602690047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
 
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa0602690047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
 
HowTo_CSS
HowTo_CSSHowTo_CSS
HowTo_CSS
 
HowTo_CSS
HowTo_CSSHowTo_CSS
HowTo_CSS
 
BloggingWithStyle_2008
BloggingWithStyle_2008BloggingWithStyle_2008
BloggingWithStyle_2008
 
BloggingWithStyle_2008
BloggingWithStyle_2008BloggingWithStyle_2008
BloggingWithStyle_2008
 
cascadingstylesheets
cascadingstylesheetscascadingstylesheets
cascadingstylesheets
 
cascadingstylesheets
cascadingstylesheetscascadingstylesheets
cascadingstylesheets
 

Último

Último (20)

Syngulon - Selection technology May 2024.pdf
Syngulon - Selection technology May 2024.pdfSyngulon - Selection technology May 2024.pdf
Syngulon - Selection technology May 2024.pdf
 
AI revolution and Salesforce, Jiří Karpíšek
AI revolution and Salesforce, Jiří KarpíšekAI revolution and Salesforce, Jiří Karpíšek
AI revolution and Salesforce, Jiří Karpíšek
 
WSO2CONMay2024OpenSourceConferenceDebrief.pptx
WSO2CONMay2024OpenSourceConferenceDebrief.pptxWSO2CONMay2024OpenSourceConferenceDebrief.pptx
WSO2CONMay2024OpenSourceConferenceDebrief.pptx
 
SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...
SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...
SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...
 
The Metaverse: Are We There Yet?
The  Metaverse:    Are   We  There  Yet?The  Metaverse:    Are   We  There  Yet?
The Metaverse: Are We There Yet?
 
Strategic AI Integration in Engineering Teams
Strategic AI Integration in Engineering TeamsStrategic AI Integration in Engineering Teams
Strategic AI Integration in Engineering Teams
 
Enterprise Knowledge Graphs - Data Summit 2024
Enterprise Knowledge Graphs - Data Summit 2024Enterprise Knowledge Graphs - Data Summit 2024
Enterprise Knowledge Graphs - Data Summit 2024
 
Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...
Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...
Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...
 
10 Differences between Sales Cloud and CPQ, Blanka Doktorová
10 Differences between Sales Cloud and CPQ, Blanka Doktorová10 Differences between Sales Cloud and CPQ, Blanka Doktorová
10 Differences between Sales Cloud and CPQ, Blanka Doktorová
 
Simplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdf
Simplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdfSimplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdf
Simplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdf
 
Buy Epson EcoTank L3210 Colour Printer Online.pptx
Buy Epson EcoTank L3210 Colour Printer Online.pptxBuy Epson EcoTank L3210 Colour Printer Online.pptx
Buy Epson EcoTank L3210 Colour Printer Online.pptx
 
PLAI - Acceleration Program for Generative A.I. Startups
PLAI - Acceleration Program for Generative A.I. StartupsPLAI - Acceleration Program for Generative A.I. Startups
PLAI - Acceleration Program for Generative A.I. Startups
 
The Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdf
The Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdfThe Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdf
The Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdf
 
Integrating Telephony Systems with Salesforce: Insights and Considerations, B...
Integrating Telephony Systems with Salesforce: Insights and Considerations, B...Integrating Telephony Systems with Salesforce: Insights and Considerations, B...
Integrating Telephony Systems with Salesforce: Insights and Considerations, B...
 
UiPath Test Automation using UiPath Test Suite series, part 2
UiPath Test Automation using UiPath Test Suite series, part 2UiPath Test Automation using UiPath Test Suite series, part 2
UiPath Test Automation using UiPath Test Suite series, part 2
 
UiPath Test Automation using UiPath Test Suite series, part 1
UiPath Test Automation using UiPath Test Suite series, part 1UiPath Test Automation using UiPath Test Suite series, part 1
UiPath Test Automation using UiPath Test Suite series, part 1
 
What's New in Teams Calling, Meetings and Devices April 2024
What's New in Teams Calling, Meetings and Devices April 2024What's New in Teams Calling, Meetings and Devices April 2024
What's New in Teams Calling, Meetings and Devices April 2024
 
Introduction to Open Source RAG and RAG Evaluation
Introduction to Open Source RAG and RAG EvaluationIntroduction to Open Source RAG and RAG Evaluation
Introduction to Open Source RAG and RAG Evaluation
 
IESVE for Early Stage Design and Planning
IESVE for Early Stage Design and PlanningIESVE for Early Stage Design and Planning
IESVE for Early Stage Design and Planning
 
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptxIOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
 

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!