Introduction to Ruby, MVC, and the Rails Framework · 2018-12-13 · MVC as used in Rails MODELS,...

19
Introduction to Ruby, MVC, and the Rails Framework Professor Larry Heimann Application Design & Development Information Systems Program

Transcript of Introduction to Ruby, MVC, and the Rails Framework · 2018-12-13 · MVC as used in Rails MODELS,...

Page 1: Introduction to Ruby, MVC, and the Rails Framework · 2018-12-13 · MVC as used in Rails MODELS, VIEWS, AND CONTROLLERS 24 Figure 2.2: Rails and MVC Ruby on Rails is an MVC framework,

Introduction to Ruby, MVC, and the Rails Framework

Professor Larry HeimannApplication Design & DevelopmentInformation Systems Program

Page 2: Introduction to Ruby, MVC, and the Rails Framework · 2018-12-13 · MVC as used in Rails MODELS, VIEWS, AND CONTROLLERS 24 Figure 2.2: Rails and MVC Ruby on Rails is an MVC framework,

Philosophy of Ruby

“For me, the purpose of life is, at least partly, to have joy. Programmers often feel joy when they can concentrate on the creative side of programming, so Ruby is designed to make programmers happy.”

— Yukihiro Matsumoto

Page 3: Introduction to Ruby, MVC, and the Rails Framework · 2018-12-13 · MVC as used in Rails MODELS, VIEWS, AND CONTROLLERS 24 Figure 2.2: Rails and MVC Ruby on Rails is an MVC framework,

Three Principles

1. Conciseness—Writing code in Ruby should involve the minimum amount of commands necessary. Code should be terse but also understandable.

2. Consistency—Ruby coding should follow common conventions that make coding intuitive and unambiguous.

3. Flexibility—There is no one right way. You should be able to pick the best approach for your needs and be able to even modify the base classes if necessary.

These three together lead to an important concept in Ruby — the principle of least surprise.

Page 4: Introduction to Ruby, MVC, and the Rails Framework · 2018-12-13 · MVC as used in Rails MODELS, VIEWS, AND CONTROLLERS 24 Figure 2.2: Rails and MVC Ruby on Rails is an MVC framework,

Comic of the Day...

Page 5: Introduction to Ruby, MVC, and the Rails Framework · 2018-12-13 · MVC as used in Rails MODELS, VIEWS, AND CONTROLLERS 24 Figure 2.2: Rails and MVC Ruby on Rails is an MVC framework,

The Ruby Way

Page 6: Introduction to Ruby, MVC, and the Rails Framework · 2018-12-13 · MVC as used in Rails MODELS, VIEWS, AND CONTROLLERS 24 Figure 2.2: Rails and MVC Ruby on Rails is an MVC framework,

Everything is an object

Looking at Strings, we see:

Page 7: Introduction to Ruby, MVC, and the Rails Framework · 2018-12-13 · MVC as used in Rails MODELS, VIEWS, AND CONTROLLERS 24 Figure 2.2: Rails and MVC Ruby on Rails is an MVC framework,

Revising the String class

Page 8: Introduction to Ruby, MVC, and the Rails Framework · 2018-12-13 · MVC as used in Rails MODELS, VIEWS, AND CONTROLLERS 24 Figure 2.2: Rails and MVC Ruby on Rails is an MVC framework,

Destructive and Predicate methods

Page 9: Introduction to Ruby, MVC, and the Rails Framework · 2018-12-13 · MVC as used in Rails MODELS, VIEWS, AND CONTROLLERS 24 Figure 2.2: Rails and MVC Ruby on Rails is an MVC framework,

Architecting Software

• Needs to be:

• understandable

• extensible

• Many different architecture patterns exist

• Model-View-Controller (MVC) one of the most popular

Page 10: Introduction to Ruby, MVC, and the Rails Framework · 2018-12-13 · MVC as used in Rails MODELS, VIEWS, AND CONTROLLERS 24 Figure 2.2: Rails and MVC Ruby on Rails is an MVC framework,

MVC is like ...

Page 11: Introduction to Ruby, MVC, and the Rails Framework · 2018-12-13 · MVC as used in Rails MODELS, VIEWS, AND CONTROLLERS 24 Figure 2.2: Rails and MVC Ruby on Rails is an MVC framework,

Model: Taking Care of Business

Page 12: Introduction to Ruby, MVC, and the Rails Framework · 2018-12-13 · MVC as used in Rails MODELS, VIEWS, AND CONTROLLERS 24 Figure 2.2: Rails and MVC Ruby on Rails is an MVC framework,

View: Looking Good

Page 13: Introduction to Ruby, MVC, and the Rails Framework · 2018-12-13 · MVC as used in Rails MODELS, VIEWS, AND CONTROLLERS 24 Figure 2.2: Rails and MVC Ruby on Rails is an MVC framework,

View: Partials

Page 14: Introduction to Ruby, MVC, and the Rails Framework · 2018-12-13 · MVC as used in Rails MODELS, VIEWS, AND CONTROLLERS 24 Figure 2.2: Rails and MVC Ruby on Rails is an MVC framework,

Controller: Holding It All Together

Page 15: Introduction to Ruby, MVC, and the Rails Framework · 2018-12-13 · MVC as used in Rails MODELS, VIEWS, AND CONTROLLERS 24 Figure 2.2: Rails and MVC Ruby on Rails is an MVC framework,

Controllers: Too Fat To Be Useful

Page 16: Introduction to Ruby, MVC, and the Rails Framework · 2018-12-13 · MVC as used in Rails MODELS, VIEWS, AND CONTROLLERS 24 Figure 2.2: Rails and MVC Ruby on Rails is an MVC framework,

Controllers: Variations

Page 17: Introduction to Ruby, MVC, and the Rails Framework · 2018-12-13 · MVC as used in Rails MODELS, VIEWS, AND CONTROLLERS 24 Figure 2.2: Rails and MVC Ruby on Rails is an MVC framework,

Controller: Traffic Cop

Page 18: Introduction to Ruby, MVC, and the Rails Framework · 2018-12-13 · MVC as used in Rails MODELS, VIEWS, AND CONTROLLERS 24 Figure 2.2: Rails and MVC Ruby on Rails is an MVC framework,

MVC as used in Rails

MODELS, VIEWS, AND CONTROLLERS 24

Figure 2.2: Rails and MVC

Ruby on Rails is an MVC framework, too. Rails enforces a structure for yourapplication—you develop models, views, and controllers as separate chunks offunctionality and it knits them all together as your program executes. One ofthe joys of Rails is that this knitting process is based on the use of intelligentdefaults so that you typically don’t need to write any external configurationmetadata to make it all work. This is an example of the Rails philosophy offavoring convention over configuration.

In a Rails application, incoming requests are first sent to a router, whichworks out where in the application the request should be sent and how therequest itself should be parsed. Ultimately, this phase identifies a particularmethod (called an action in Rails parlance) somewhere in the controller code.The action might look at data in the request itself, it might interact with themodel, and it might cause other actions to be invoked. Eventually the actionprepares information for the view, which renders something to the user.

Figure 2.2, shows how Rails handles an incoming request. In this example, theapplication has previously displayed a product catalog page and the user hasjust clicked the Add To Cart button next to one of the products. This buttonlinks to http://my.url/store/add_to_cart/123, where add_to_cart is an action in ourapplication and 123 is our internal id for the selected product.1

1. We cover the format of Rails URLs later in the book. However, it’s worth pointing out here thathaving URLs perform actions such as add to cart can be dangerous. See Section 21.6, The Problem

with GET Requests, on page 463 for more details.

Report erratumPrepared exclusively for Sharon Blazevich

Page 19: Introduction to Ruby, MVC, and the Rails Framework · 2018-12-13 · MVC as used in Rails MODELS, VIEWS, AND CONTROLLERS 24 Figure 2.2: Rails and MVC Ruby on Rails is an MVC framework,

Class Exercise

To know the Model-View-Controller, you must be the

Models, Views and Controllers...