1 dr alexiei dingli web science stream introducing rails

Post on 20-Jan-2016

212 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

1

Dr Alexiei Dingli

Web Science Stream

Introducing Rails

2

• Open source web application framework for Ruby

• Intended for agile programming

• Create by David Heinemeier Hansson

• Released originally in 2004 but only became popular 2007 when it was distributed with Mac OS X Leopard

What is Rails?

3

• Development

• Testing

• Production

3 Environments

4

• Changes to source code immediately visible (just reload)

• Speed is not critical

• Provide insight to developer– Errors are easily spotted by showing the stack

trace

Development

5

• Fill database using dummy data

• Unit and functional testing are fully automated

• Invoked from the command line

Testing

6

• Update to code base infrequent

• Production environment can be optimised

• Focus on performance

• Errors can be sent by email to the developer

Production

7

/config/Database.yml

development:

adapter: sqlite3

database: db/development.sqlite3

timeout: 5000

test:

adapter: sqlite3

database: db/test.sqlite3

timeout: 5000

production:

adapter: mysql

database: newMySqlProductionDB

username: root

password: superSecretPassword

timeout: 5000

Setting our database

8

Database Server

Development

Table 1 Table 2

Test

Table 1 Table 2

Production

Table 1 Table 2

Database architecture

9

• Models– Handling data and business logic

• Controllers– Handling the user interface and application

logic

• Views– Handling GUI and persistent objects

Model-View-Controller Architecture

10

MVC Process

11

• Improves scalability– Upgrade parts of the system without changing other

components (Eg just the database)

• Makes maintenance easier– Components have low dependency on each other

• Promotes reuse– Model may be reused by multiple views (or vice-

versa)

Why MVC?

12

app

controllers

helpers

models

views

MVC in Rails

13

• Called Active Records– Connect to database– Retrieve data from tables– Store data in tables

• Abstracts from the database– Can connect immediately to SQLite, MySQL

and PostgreSQL– RubyGems provide access to other

Databases

The model

14

• Active Records abstract database specific code thus making it easy to shift between different databases

• Another magic of Rails is migration which saves us from creating any SQL

Database Abstraction

15

• Active Controller– Is the glue between the application data,

presentation layer and the web browser– Decides how to handle a request– Retrieve data from the model to be passed– Gather information from browser request

The controller

16

• Class names should be written with each word beginning with a capital letter and no spaces between words– StoryBook– AgeCalculator

• Filenames are written in lowercase with undderscores separating each word– story_book– age_calculator

Some conventions

17

• Action View– Should only contain presentation logic– Can contain HTML and also Ruby code using

the embded Ruby (ERb) syntax

<b><%= “Hello World!” %></b>

The View

18

• The template has a one-to-one mapping to the action of a controller– Add should be in Add.html.erb

• The folder that stores the template should be named after the controller

Views conventions

19

html.erb

Standard HTML template which might contan ERb tags

xml.builder

Output XML (Eg RSS feeds)

js.rjs

Return JavaScript instructions

Views extensions

20

• REpresentational State Transfer – Design pattern not restricted to the WWW– Offers a resource centric approach where

• Client uses a resource• Every resource needs to be addressed uniquely• Interactions with resources are exposed as

operations• Most common are the CRUD operations

– Create, Read, Update, Delete

Lets have some REST

21

• The protocol used is– Stateless

• Each request to the server is completely independent

– Cacheable• Proxies

– Layered• Routers• Firewalls

More REST

22

CRUD HTTP

CREATE POST

READ GET

UPDATE PUT (Not implemented)

DELETE DELETE (Not implemented)

RESTing on the WWW

23

• Deep relationship to provide assistance for easy construction of URLs

• Rails uses modified POST requests to implement the PUT and DELETE methods

The REST of Rails

24

• Rails generate the application’s structure easily

• Simply use the generate script

• Try – ruby script/generate

Magical Generation

25

• Convention over configuration– Developers only specify unconventional

aspects– Eg if we have a class Sale in the model, the

table will be called sales automatically– Leads to less code and less repetition– If the convention is not kept then more

programming is needed

Rails Philosophy (1)

26

• Don’t Repeat Yourself (DRY)– Information located in a single unambiguous

place– Eg using Active Records, no columns or

tables are specified, everything is done automatically.

Rails Philosophy (2)

27

Questions?

top related