Ruby on Rails

bricklayerbelchedInternet and Web Development

Feb 5, 2013 (4 years and 4 months ago)

104 views

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




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

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

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



T

O

M

C

A

T


ROR MVC Architecture



W
E

B


C

L

I

E

N

T

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



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



<h1> Hello from Rails! </h1>




-

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





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
<%

and
%>




-

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 <%= t =
Time.now

%>


Number of seconds since midnight:


<%=
t.hour

* 3600 + t.min * 60 + t.sec %>


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

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:



Chapter 15
© 2010 by Addison Wesley Longman, Inc.


10

15.3 Rails Applications with


Databases
(continued)


<h1>Listing corvettes</h1>

<table>


<tr>


<th>Body style</th>


<th>Miles</th>


<th>Year</th>


</tr>

<% for corvette in @corvettes %>


<tr>


<td><%=h corvette.body_style %></td>


<td><%=h corvette.miles %></td>


<td><%=h corvette.year %></td>


<td><%= link_to 'Show', corvette %></td>


<td><%= link_to 'Edit',


edit_corvette_path(corvette) %></td>


<td><%= link_to 'Destroy', corvette,


:confirm => 'Are you sure?',


:method => :delete %></td>


</tr>

<% end %>

</table>

<br />

<%= link_to 'New corvette', new_corvette_path %>