4
Web Application Architectures Module 5: Middleware Lecture 8: MVC Implementation in Rails c 2011-13 G.L. Heileman Module 5, Lecture 8 1/4

M5 l8-mvc-rails-handout

Embed Size (px)

Citation preview

Page 1: M5 l8-mvc-rails-handout

Web Application ArchitecturesModule 5: Middleware

Lecture 8: MVC Implementation in Rails

c© 2011-13 G.L. Heileman Module 5, Lecture 8 1 / 4

Page 2: M5 l8-mvc-rails-handout

Ruby on Rails

In Rails, the following classes support the MVC design pattern:

Model – The ActiveRecord class, described previously, implementsobject-relational mappings (ORM).

View – Views and controllers are actually bundled together in Rails inAction Pack. Dynamic content is generated by templates containingembedded Ruby (ERb) code supported by the ActionView class.

Controller – The ActionController class is the core of a web request inRails. It consists of one or more actions that are executed on requestand then either render a template or redirect to another action. Anaction is defined as a public method on the controller, which willautomatically be made accessible to the web-server through RailsRoutes.

c© 2011-13 G.L. Heileman Module 5, Lecture 8 2 / 4

Page 3: M5 l8-mvc-rails-handout

MVC Interactions in Rails

1 The browser sends a request to the web server.

1 The browser sends a request to the web server.2 The web server processes the request, determines which route it

belongs to and dispatches that request to the corresponding controllermethod.

1 The browser sends a request to the web server.2 The web server processes the request, determines which route it

belongs to and dispatches that request to the corresponding controllermethod.

3 The controller asks the model layer for all of the information neededto complete the request.

1 The browser sends a request to the web server.2 The web server processes the request, determines which route it

belongs to and dispatches that request to the corresponding controllermethod.

3 The controller asks the model layer for all of the information neededto complete the request.

4 The model layer collects the requested information and returns it tothe controller.

1 The browser sends a request to the web server.2 The web server processes the request, determines which route it

belongs to and dispatches that request to the corresponding controllermethod.

3 The controller asks the model layer for all of the information neededto complete the request.

4 The model layer collects the requested information and returns it tothe controller.

5 The controller gives the appropriate information to the view,requesting a particular format.

1 The browser sends a request to the web server.2 The web server processes the request, determines which route it

belongs to and dispatches that request to the corresponding controllermethod.

3 The controller asks the model layer for all of the information neededto complete the request.

4 The model layer collects the requested information and returns it tothe controller.

5 The controller gives the appropriate information to the view,requesting a particular format.

6 The view renders the information in that format and gives it back tothe controller.

1 The browser sends a request to the web server.2 The web server processes the request, determines which route it

belongs to and dispatches that request to the corresponding controllermethod.

3 The controller asks the model layer for all of the information neededto complete the request.

4 The model layer collects the requested information and returns it tothe controller.

5 The controller gives the appropriate information to the view,requesting a particular format.

6 The view renders the information in that format and gives it back tothe controller.

7 The controller assembles the total page’s html and gives it to the webserver.

1 The browser sends a request to the web server.2 The web server processes the request, determines which route it

belongs to and dispatches that request to the corresponding controllermethod.

3 The controller asks the model layer for all of the information neededto complete the request.

4 The model layer collects the requested information and returns it tothe controller.

5 The controller gives the appropriate information to the view,requesting a particular format.

6 The view renders the information in that format and gives it back tothe controller.

7 The controller assembles the total page’s html and gives it to the webserver.

8 The web server returns the page to the browser.

Browser

Web Server

1 http://localhost:3000/posts(GET request)

Browser

Web Server

Routes

Dispatcher

Controller

1

2

http://localhost:3000/posts

PostsController::index

resources :posts

(GET request)

Browser

Web Server

Routes

Dispatcher

Controller

Model

1

2

http://localhost:3000/posts

PostsController::index

3 @posts = Post.all

resources :posts

(GET request)

Browser

Web Server

Routes

Dispatcher

Controller

Model

1

2

http://localhost:3000/posts

PostsController::index

3 @posts = Post.all4 Post array

resources :posts

(GET request)

Browser

Web Server

Routes

Dispatcher

Controller

Model View

1

2

http://localhost:3000/posts

PostsController::index

3 @posts = Post.all 5 @posts4 Post array

resources :posts

(GET request)

Browser

Web Server

Routes

Dispatcher

Controller

Model View

1

2

http://localhost:3000/posts

PostsController::index

3 @posts = Post.all 5 @posts4 Post array 6 index.html.erb

resources :posts

(GET request)

Browser

Web Server

Routes

Dispatcher

Controller

Model View

1

2

http://localhost:3000/posts

PostsController::index

3 @posts = Post.all 5 @posts4 Post array 6 index.html.erb

resources :posts7 application.html.erb

+ index.html

(GET request)

Browser

Web Server

Routes

Dispatcher

Controller

Model View

1

2

http://localhost:3000/posts

PostsController::index

3 @posts = Post.all 5 @posts4 Post array

8 text/html

6 index.html.erb

resources :posts7 application.html.erb

+ index.html

(response)(GET request)

c© 2011-13 G.L. Heileman Module 5, Lecture 8 3 / 4

Page 4: M5 l8-mvc-rails-handout

MVC Interactions in Rails

Browser

Web Server

Routes

Dispatcher

Controller

Model View

1

2

http://localhost:3000/posts

PostsController::index

3 @posts = Post.all 5 @posts4 Post array

8 text/html

6 index.html.erb

resources :posts7 application.html.erb

+ index.html

(response)(GET request)

c© 2011-13 G.L. Heileman Module 5, Lecture 8 4 / 4