36
September 8, 2015 Ruby on Rails Overview and Architecture 1

Ruby on Rails - tanli.org · • The main type of view in a Rails application is a “template” • Templates usually map directly to an associated controller action • “New”,

  • Upload
    ngoliem

  • View
    216

  • Download
    0

Embed Size (px)

Citation preview

September 8, 2015

Ruby on RailsOverview and Architecture

1

Who Am I?

2

Assumptions

• Passing familiarity with Ruby or equivalent dynamic language fundamentals (duck-typing, run-time object alteration, etc.)

• Knowledge of object-oriented programming (OOP)

• Attention span >= gnat

3

Objectives

• Learn the basics of a Rails application

• Learn the basics of “Convention over Configuration”

• Learn the basics of the Model-View-Controller (MVC) architecture

• Learn the basics of a RESTful application/service

• Understand how Rails templates and views work

4

What We Won’t Cover

• Ruby syntax and conventions

• Ruby API

• Rails framework API/CLI/Gemsets

• Rails Middleware and Rack

• Advanced environment configuration

• Database adapters, querying, and management

• Design Patterns

5

The Rails Framework

6

What is “Rails”?

• Rails is a web application framework built upon, and written in, the Ruby programming language.

• Open source

• Easy to learn; difficult to master.

• Fun (and a time-saver)!

• Utilizes a Model-View-Controller (MVC) architecture (more on that in a moment).

7

Rails Ideologies and Principles

8

Principles

• Rails operates under many principles, but here are my top two:

• DRY - “Don’t Repeat Yourself”

• Use proper abstraction for implementations to avoid redundancy (e.g. presenters and decorators)

• CoC - “Convention over Configuration”

• Many things are configured automatically by virtue of file structures and class names

9

Convention over Configuration

10

CoC

• This constitutes a vast majority of any Rails app.

• The most obvious form of CoC is seen in the models, views, controllers, and database schema.

• To wit: scaffolding and generators.

11

Model-View-Controller (MVC)

12

What is MVC?

• MVC is the architecture upon which Rails operates.

• It breaks down the web application into three distinct (but not necessarily discrete) entities:

• The models (objects)

• The views (visual interfaces)

• The controllers (communication between models and views)

13

14

MVC: How it Works

1. A user visits a site and makes a request.

2. The dispatcher routes the user’s request to the appropriate controller.

3. The controller interacts with the models in the application to fetch, alter, create, or store data within the database based on the user’s input/request.

4. The view requested by the user is passed back to the controller and rendered in their browser, which contains the information from the models.

5. Rinse and repeat.

15

MVC: Model

• Models are classes. They represent objects used by the Rails application.

• Model names are often linked directly to a table name in a database (line 1).

fig 1: basic rails model with validators and relationships

16

MVC: Model (cont)

• Basic anatomy of a model:

• ...validations for data used to populate the database (line 3).

• ...associations with other models (lines 5-6).

• ...user-defined instance methods (not shown).

fig 1: basic rails model with validators and relationships

17

MVC: Controller

• Acts as an intermediary between the models and the views.

• Responds to user interactions from the user interface and performs operations on the models.

• Each action in a controller is [typically] linked to a default template in the Rails application.

fig 3: basic rails controller with default actions

18

MVC: Controller (cont)

• Controllers should include all RESTful actions by default until you know better.

• Actions can be omitted if necessary.

• Controllers can include non-RESTful actions.

• They can also respond to multiple formats (XML, JSON, HTML)

fig 3: basic rails controller with default actions

19

Views

20

MVC: View

• The actual user interface of the application.

• Used to display and interact with data from the database.

• Default file format: HTML ERB (HTML with embedded Ruby)

fig 3: basic Rails view in “erb” format

21

MVC: View (cont)

• Views come in many (default) flavors:

• “*.html” - Standard HTML

• “*.erb” - Embedded Ruby

• “*.html.erb” - HTML with Embedded Ruby

• “_*.html.erb” - A partial (note the leading underscore)

• “*.json” - JSON-encoded string representing one or more models

22

MVC: View (JSON)

• JSON views may be:

• Visual - RSS feeds and standard HTML (tabular data, etc)

• Or non-visual - Application API

• They have other advantages as well:

• Rails applications can use JSON views to serve the main website, thereby eliminating the need for two sets of controllers (one web-based, another for API requests)

23

MVC: View (cont)

• The main type of view in a Rails application is a “template”

• Templates usually map directly to an associated controller action

• “New”, “Edit”, “Show”, or “Index”

• Another type of view is called a “partial”

• These are reusable view snippets

24

MVC: View (anatomy)

fig 4: “index.html.erb”

25

MVC: View (templates)

fig 4: “index.html.erb”

26

MVC: View (templates)

fig 4: “index.html.erb”

27

MVC: View (partials)

fig 5: “_product_fields.html.erb”

28

MVC: View (do’s and don’ts)

• Inline CSS styling

• Inline template style blocks (“<style></style>”)

• Inline Javascript

• Inline template Javascript blocks (“<script></script>”)

29

30

Rails: Routes

fig 6: a route in the routes file

31

Words of Wisdom

• Work smart: if something simple is taking too long, there is likely a better way.

• ASK QUESTIONS - if you do not understand, say so (it saves time and effort).

• TEST YOUR CODE - never, ever, submit a feature or patch without thoroughly testing it. Write automated tests or test by hand.

32

Just in Case...

• DID I MENTION THAT YOU SHOULD ASK QUESTIONS?

• HERE’S A SLIDE DEDICATED TO THAT, JUST IN CASE THE POINT WAS LOST OR MISSED.

• ASK QUESTIONS.

33

Resources

• Rails Guides (guides.rubyonrails.org)

• Rails API (api.rubyonrails.org)

• Railscasts (railscasts.com - “pro” videos require a subscription, sold separately)

• Stack Overflow (stackoverflow.com)

• Yourself, your friends, instructors, advisors, people like me

34

Resources (environment)

• Sublime text (editor, not an IDE)

• Rubymine (IDE, resource-intensive, not free)

• Rails Console (“rails c” on command-line)

• Web inspection tools

35