• No results found

A Model of the Operation of The Model-View- Controller Pattern in a Rails-Based Web Server

N/A
N/A
Protected

Academic year: 2021

Share "A Model of the Operation of The Model-View- Controller Pattern in a Rails-Based Web Server"

Copied!
21
0
0

Loading.... (view fulltext now)

Full text

(1)

A Model of the Operation of

The

Model-View-

Controller

Pattern

(2)
(3)

Model-View-Controller pattern on a web server Dubberly Design Office

January 3, 2011

Remote server (Computer)

Ruby on Rails Framework

Model Controller View Routes file Browser Page User Database

Web Server (App)

A user clicks a link to a page in a web application.

clicks link

DNS (Domain Name System) parses the URL and routes request to the server.

http://domain.com/resource/action/argument

requests resource from Page 1

(4)

Model-View-Controller pattern on a web server Dubberly Design Office

January 3, 2011

Remote server (Computer)

Ruby on Rails Framework

Model-View-Controller pattern on a web server Dubberly Design Office

January 3, 2011 Model Controller View Routes file Browser Page User Database

Web Server (App)

Model Controller View Routes file Browser Page User Database

Web Server (App)

The web server receives the request URL (Universal Resource Location). Rails uses a routes file to match the URL with a controller action.

The routes file contains a list of URL patterns. Each pattern maps to a resource and action.

One conventional URL pattern has a server resource, an action to be taken on the resource,

and arguments for the action, separated by slashes: http://domain.com/resource/action/argument More on resources later.

passes request to

activates a

(5)

Model-View-Controller pattern on a web server Dubberly Design Office

January 3, 2011

Remote server (Computer)

Ruby on Rails Framework

Model-View-Controller pattern on a web server Dubberly Design Office

January 3, 2011 Model Controller View Routes file Browser Page User Database

Web Server (App)

The invoked controller action requests data from a model.

The model queries the database and hands data back to the controller.

Model Controller View Routes file Browser Page User Database

Web Server (App)

requests data from a

passes data to

queries passes data to

(6)

Model-View-Controller pattern on a web server Dubberly Design Office

January 3, 2011

Remote server (Computer)

Ruby on Rails Framework

Model-View-Controller pattern on a web server Dubberly Design Office

January 3, 2011 Model Controller View Routes file Browser Page User Database

Web Server (App)

The controller action then passes data to a corresponding view. The view uses the data and a template to compose a page.

Model Controller View Routes file Browser Page User Database

Web Server (App)

passes data to

renders page from template and passes it to

(Page renderer)

Don’t mistake the view, a template, and a page. It may help to think of the view as “page composer”. Rails includes the page composer.

Developers make templates.

The view uses a template to compose a page.

(7)

Model-View-Controller pattern on a web server Dubberly Design Office

January 3, 2011

Remote server (Computer)

Ruby on Rails Framework

Model-View-Controller pattern on a web server Dubberly Design Office

January 3, 2011 Model Controller View Routes file Browser Page User Database

Web Server (App)

Model Controller View Routes file Browser Page User Database

Web Server (App)

Model Controller View Routes file Browser Page User Database

Web Server (App)

The controller passes the complete page to the web server.

passes page to

(8)

Model-View-Controller pattern on a web server Dubberly Design Office

January 3, 2011

Remote server (Computer)

Ruby on Rails Framework

Model-View-Controller pattern on a web server Dubberly Design Office

January 3, 2011 Model Controller View Routes file Browser Page User Database

Web Server (App)

The web server serves the page to the browser.

The browser renders the new page in place of the first one.

Model Controller View Routes file Browser Page User Database

Web Server (App)

serves page to displayed for

(9)

Model-View-Controller pattern on a web server Dubberly Design Office

January 3, 2011

Remote server (Computer)

Ruby on Rails Framework

Model-View-Controller pattern on a web server Dubberly Design Office

January 3, 2011 Model Controller View Routes file Browser Page User Database

Web Server (App)

Ruby on Rails provides a framework for this MVC flow.

It enables developers to work on what makes their apps unique rather than spend time re-implementing conventions.

Model Controller View Routes file Browser Page User Database

Web Server (App) clicks link

requests resource from

passes request to

activates a

requests data from a

passes data to

queries passes data to

passes data to

passes page to serves page to displayed for

renders page from template and passes it to

(10)

Model-View-Controller pattern on a web server Dubberly Design Office

January 3, 2011

Remote server (Computer)

Ruby on Rails Framework

Model-View-Controller pattern on a web server Dubberly Design Office

January 3, 2011

Composed of HTML/CSS/JavaScript E.g. Firefox, Chrome, Safari

E.g. MySQL

Written in Ruby language

Templates written in Ruby and HTML. Views compose templates into pages which link to CSS and JavaScript. Written in Ruby language

Written in Rails routing DSL (Domain Specific Language)

E.g. Apache

Model-View-Controller pattern on a web server Dubberly Design Office

January 3, 2011 Model Controller View Routes file Browser Page User Database

Web Server (App)

Developers write components in a variety of languages.

Model Controller View Routes file Browser Page User Database

(11)

Model-View-Controller pattern on a web server Dubberly Design Office

January 3, 2011

Remote server (Computer)

Ruby on Rails Framework

Model-View-Controller pattern on a web server Dubberly Design Office

January 3, 2011 Model Controller View Routes file Browser Page User Database

Web Server (App)

A model with a controller and a route is called a resource. Resources are named with nouns.

In a health-related application you may find resources such as:

Person Vital Goal

Prescription

(12)

Model-View-Controller pattern on a web server Dubberly Design Office

January 3, 2011

Remote server (Computer)

Ruby on Rails Framework

Model-View-Controller pattern on a web server Dubberly Design Office

January 3, 2011 Model Controller View Routes file Browser Page User Database

Web Server (App)

Attributes (or data) of a resource are accessed through the model. Attributes (model methods) are also named with nouns.

A person model may have methods such as:

Person.date_of_birth Person.height Person.gender Person.allergies Model Controller View Routes file Browser Page User Database

Web Server (App)

Person Person Resource

(13)

Model-View-Controller pattern on a web server Dubberly Design Office

January 3, 2011

Remote server (Computer)

Ruby on Rails Framework

Model-View-Controller pattern on a web server Dubberly Design Office

January 3, 2011 Model Controller View Routes file Browser Page User Database

Web Server (App)

Actions available to a resource are accessed through the controller. Actions (controller methods) are named with verbs.

Controllers typically have one or more standard methods such as:

PersonController.create PersonController.show PersonController.update PersonController.destroy Model Controller View Routes file Browser Page User Database

Web Server (App)

PersonController

Person

(14)

Model-View-Controller pattern on a web server Dubberly Design Office

January 3, 2011

Remote server (Computer)

Ruby on Rails Framework

Model-View-Controller pattern on a web server Dubberly Design Office

January 3, 2011 Model Controller View Routes file Browser Page User Database

Web Server (App)

Resource actions may be visualized in a page, from a view template. Names of view templates correspond to controller methods.

Templates corresponding to standard controller methods are:

person/create.html.erb person/show.html.erb person/update.html.erb person/destroy.html.erb Model Controller View Routes file Browser Page User Database

Web Server (App)

PersonController

Person person/show.html.erb Person Resource

(15)
(16)

Early web applications sent a page from the server after every user interaction.

Now, using AJAX (Asynchronous JavaScript and XML) techniques, it is possible to send and receive data and fragments of pages,

increasing responsiveness.

AJAX involves sending or requesting data from a server “asynchronously”—in the background—

while the user may continue to interact with the page. The page may be altered when the server responds. (The “page” also caches some data locally

(17)

Model-View-Controller pattern on a web server Dubberly Design Office

January 3, 2011

Remote server (Computer)

Ruby on Rails Framework

A user action triggers a request for additional data from a server.

Model Controller View Routes file Browser Page User Database

Web Server (App) clicks link

requests resource from Page 1

(18)

Model-View-Controller pattern on a web server Dubberly Design Office

January 3, 2011

Remote server (Computer)

Ruby on Rails Framework

In the same way a page request is processed, the web server uses route definitions

to pass the data request to the appropriate controller action. Model-View-Controller pattern on a web server

Dubberly Design Office January 3, 2011 Model Controller View Routes file Browser Page User Database

Web Server (App)

Model Controller View Routes file Browser Page User Database

Web Server (App) passes request to

(19)

Model-View-Controller pattern on a web server Dubberly Design Office

January 3, 2011

Remote server (Computer)

Ruby on Rails Framework

The controller requests data from a model.

The model queries the database and hands data back to the controller. Model-View-Controller pattern on a web server

Dubberly Design Office January 3, 2011 Model Controller View Routes file Browser Page User Database

Web Server (App)

Model Controller View Routes file Browser Page User Database

Web Server (App)

requests data from a

passes data to

(20)

Model-View-Controller pattern on a web server Dubberly Design Office

January 3, 2011

Remote server (Computer)

Ruby on Rails Framework

Rather than rendering a page via a view,

the controller formats the data as XML or JSON (JavaScript Object Notation)

and hands it to the web server.

Model-View-Controller pattern on a web server Dubberly Design Office

January 3, 2011 Model Controller View Routes file Browser Page User Database

Web Server (App)

Model Controller View Routes file Browser Page User Database

Web Server (App)

Model Controller View Routes file Browser Page User Database

Web Server (App)

(21)

Model-View-Controller pattern on a web server Dubberly Design Office

January 3, 2011

Remote server (Computer)

Ruby on Rails Framework

Model-View-Controller pattern on a web server Dubberly Design Office

January 3, 2011 Model Controller View Routes file Browser Page User Database

Web Server (App)

The web server’s response is rendered in the current page.

Model Controller View Routes file Browser Page User Database

Web Server (App)

serves data to displayed for

References

Related documents

Veracode’s Vendor Application Security Testing (VAST) Program provides the first comprehensive application security compliance program to large enterprise customers as well as

Acquisition-related costs for the purchase of both Sky Deutschland and Sky Italia (see below) comprised advisory and transaction fees including, inter alia, financial advisory

Keywords: Temporary speed restrictions; railway maintenance; statistical modelling; Negative Binomial regression; 29..

Leveraging standards to improve the methods by which investigative sites can conduct medical research and capture data for clinical research studies is vital for a number of

The Faculty of Engineering and Applied Science (FEAS) and the Faculty of Energy Systems and Nuclear Science (FESNS) at the University of Ontario Institute of Technology (UOIT)

Physicists : origin of anomalous fluctuations in stock markets, transitions between predictable and unpredictable regimes.. Minority Games with many

Although caffeine shows a larger solubility in the granulating liquid than the APAP (Table 2), the difference in the amounts of API dissolved in the granulating liquid at

Those approaches that strategically reduce vehicle weight (using new lightweight materials to reduce weight while holding vehicle size constant and reducing the weight of the