Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 15 © 2010 by Addison Wesley Longman, Inc. 1 14.1 Origins and Uses of Ruby - Designed by Yukihiro Matsumoto; released in 1996 - Use spread rapidly.

Similar presentations


Presentation on theme: "Chapter 15 © 2010 by Addison Wesley Longman, Inc. 1 14.1 Origins and Uses of Ruby - Designed by Yukihiro Matsumoto; released in 1996 - Use spread rapidly."— Presentation transcript:

1 Chapter 15 © 2010 by Addison Wesley Longman, Inc. 1 14.1 Origins and Uses of Ruby - Designed by Yukihiro Matsumoto; released in 1996 - Use spread rapidly in Japan - Use is now growing in part because of its use in Rails - A pure object-oriented purely interpreted scripting language - Related to Perl and JavaScript, but not closely

2 Chapter 15 © 2010 by Addison Wesley Longman, Inc. 2 15.1 Overview of Rails - Rails is a development framework for Web-based applications - Rails is written in Ruby and uses Ruby for its applications - Ruby on Rails (RoR) - Based on MVC architecture for applications - MVC cleanly separates applications into three parts: - Model – the data and any restraints on it - View – prepares and presents results to the user - Controller – controls the application - One characterizing part of Rails is its approach to connecting object-oriented software with a relational database – ORM - Maps tables to classes, rows to objects, and columns to fields of the objects

3 Chapter 15 © 2010 by Addison Wesley Longman, Inc. 3 Architecture of a Four-Tier Application DBMS / Database Server Database User Interface Database Engine Supporting Software Application Server Database API Application Logic App User Interface W EB S E R V E R Architecture of a Four-Tier Application W EB C L I E N T

4 Chapter 15 © 2010 by Addison Wesley Longman, Inc. 4 Architecture of a Four-Tier Application MySQL Database User Interface Database Engine Supporting Software Rails Model Control View TOMCATTOMCAT ROR MVC Architecture WEBCLIENTWEBCLIENT

5 Chapter 15 © 2010 by Addison Wesley Longman, Inc. 5 15.1 Overview of Rails (continued) - View documents are XHTML documents that may include Ruby code - Most of the controller code is provided by Rails - A Rails application is a program that provides a response when a client browser connects to a Rails-driven Web site - Rails can be used with Ajax - Two fundamental principles that guided the development of Rails: 1. DRY 2. Convention over configuration - Rails does not use a GUI - One simple way to get started with Rails is to download a complete development system http://instantrails.rubyforge.org/wiki/wiki.pl - Includes Ruby, Rails, MySQL, Apache and everything else that is necessary

6 Chapter 15 © 2010 by Addison Wesley Longman, Inc. 6 15.2 Document Requests (continued) - Static Documents (continued) - Next, build the view file, or template, which must reside in the say subdirectory of the views directory of the application and be named hello.html.erb - This is an XHTML document with the following in its body element Hello from Rails! - To test the application, a Web server must be started >ruby script/server - This starts the default server, named Mongrel - Now, pointing the browser to the application produces the displayed view template content

7 Chapter 15 © 2010 by Addison Wesley Longman, Inc. 7 15.2 Document Requests (continued) - Dynamic Documents - Dynamic documents can be built with Rails by embedding Ruby code in the template document - An example: display a greeting and the current date and time and the number of seconds since midnight - Ruby code is embedded in a document by placing it between - To insert the result of evaluating the code into the document, use <%= - The Time class has a method, now, that returns the current day of the week, month, day of the month, time, time zone, and year, as a string It is now Number of seconds since midnight:

8 Chapter 15 © 2010 by Addison Wesley Longman, Inc. 8 15.3 Rails Applications with Databases - We will use MySQL - Use a simple database with just one table - The application will be named cars - The application will present a welcome document to the user, including the number of cars in the database and a form to get the beginning and ending years and a body style for the desired car - Creating the application - In the subdirectory of our examples: >rails –d mysql cars - It is customary to use three databases in a Rails database application: one for development, one for testing, and one for production - To create the three (empty) databases: >rake db:create:all - Creates the database.yml file in the config subdirectory of the application directory ( cars ) - See next page

9 Chapter 15 © 2010 by Addison Wesley Longman, Inc. 9 15.3 Rails Applications with Databases (continued) - If we fill out the form, as in: - Now we click Create, which produces:

10 Chapter 15 © 2010 by Addison Wesley Longman, Inc. 10 15.3 Rails Applications with Databases (continued) Listing corvettes Body style Miles Year <%= link_to 'Edit', edit_corvette_path(corvette) %> <%= link_to 'Destroy', corvette, :confirm => 'Are you sure?', :method => :delete %>


Download ppt "Chapter 15 © 2010 by Addison Wesley Longman, Inc. 1 14.1 Origins and Uses of Ruby - Designed by Yukihiro Matsumoto; released in 1996 - Use spread rapidly."

Similar presentations


Ads by Google