An Introduction to Ruby On Rails

64
Ruby On Rails A Web Application Framework 1

description

An Introduction to the Web framework Ruby on Rails by Jonathan Weiss. Presented for Juniter in 2005.

Transcript of An Introduction to Ruby On Rails

Page 1: An Introduction to Ruby On Rails

Ruby On RailsA Web Application Framework

1

Page 2: An Introduction to Ruby On Rails

ÜberblickWiederholgung Ruby

Einführung in Rails

Installation und Einrichtung

“Hello World”

Real World Example

Weitere Features

Ressourcen

2

Page 3: An Introduction to Ruby On Rails

Wiederholung Ruby

3

Page 4: An Introduction to Ruby On Rails

Ruby?

Ruby ist eine vollständig objektorientierte Skriptsprache

Das Beste von Perl und Python

Public release 1995 durch Yukihiro Matsumoto, a.k.a “Matz”

4

Page 5: An Introduction to Ruby On Rails

Wichtigste Eigenschaften

Interpretierte Skriptsprache

Aktuell ist Ruby 1.8.2

Komplett Objekt-Orientiert

Dynamische Typbindung

Verfügbar über GPL oder eigene Lizenz

5

Page 6: An Introduction to Ruby On Rails

5.times do puts “ruby ist cool”.upcase end

Output:RUBY IST COOLRUBY IST COOLRUBY IST COOLRUBY IST COOLRUBY IST COOL

6

Page 7: An Introduction to Ruby On Rails

GrundregelnKein Semikolon am Ende der Zeile (bei einer Anweisung pro Zeile)

Keine geschweiften Klammern zur Deklaration vom Blöcken, sondern “xxxx......end”

Keine Deklaration von Variablen!

() können bei Methoden weggelassen werden

7

Page 8: An Introduction to Ruby On Rails

a = 9b = 1c = a + bprint c ; c = “Hi”; print cd = c.length + c.index(”i”)c = -99.absein_string = String.new(”HALLO”)auch_ein_string = ‘a’ + “bcdefg #{c}”

8

Page 9: An Introduction to Ruby On Rails

Funktionen/Methoden

Einleitung mit

def funktions_name(argument)

....

end

implizites return des letzten Ausdruckes (wie überall in Ruby)

9

Page 10: An Introduction to Ruby On Rails

def my_func puts “abc” 99enddef my_func2(a, b) return a+benddef a; “a”; end

ergebnis = my_func(1,2)a()

10

Page 11: An Introduction to Ruby On Rails

class Person @name #Instanz-var @alter #Instanz-var @@anzahl_auf_der_welt #Klassen-var MAX_ALTER = 100 #Konstante def check_alter(alter) #Instanz-meth alter < Person::MAX_ALTER endend

a = Person.newif a.check_alter(90) puts “jung genug”end 11

Page 12: An Introduction to Ruby On Rails

class Person def Person.how_many @@anzahl_auf_der_welt endend

anzahl = Person.how_many

12

Page 13: An Introduction to Ruby On Rails

Iteratoren

Iteratoren sind Funktionen, die Blocks repetitiv aufrufen

Finden dort Einsatz, wo andere Sprachen normalerweise Schleifen nutzen würden

Intuitiv zu benutzten

13

Page 14: An Introduction to Ruby On Rails

5.times { puts “Hi”}

a = [1, 2]a.each do |the_one| print the_oneend

[”H”, “A”, “L”].collect {|x| x.succ} --> [”I”, “B”, “M”]

[1, 2, 3, 5, 7, 9].find {|v| v*v > 30} --> 7

14

Page 15: An Introduction to Ruby On Rails

begin

funktion_die_schief_laufen_kann(1,2,”aaa”)

rescue StandardError

puts “Fehler aufgetreten”

ensure

puts “wird immer ausgeführt”

end

15

Page 16: An Introduction to Ruby On Rails

Ruby on Rails

16

Einführung

Page 17: An Introduction to Ruby On Rails

Ruby on Rails

Framework für Web-Anwendungen

Model-View-Controller Ansatz

Nur eine Konfigurationsdatei: Datenbank Zugangsdaten

Convention over configuration

17

Page 18: An Introduction to Ruby On Rails

Model-View-Controller

Design-Pattern für Programme mit Interaktion mit dem Benutzer

KLassen werden in drei Gruppen eingeteilt, die sich auf ihre jeweillige Aufgabe spezialisieren

Vorteil: Entkopplung und somit Wiederverwendbarkeit

18

Page 19: An Introduction to Ruby On Rails

Model

Klasse, die das Business-Objekt abbildet mit allen seinen Funktionalitäten

BSP:

Klasse Benutzer

Klasse Buch

19

Page 20: An Introduction to Ruby On Rails

ViewEigentliche Darstellung für den User

Keine Funktionalität, nur Design+Anzeige

BSP:

Template, dass alle Bücher im System anzeigt

Template, dass alle Bücher eines Benutzers anzeigt

20

Page 21: An Introduction to Ruby On Rails

ControllerVerbindung zwischen Model und View

Lädt benötigte Daten aus dem Model und füttert sie in den View

Prozess-Logik

BSP:

Lade alle Bücher im System und gebe sie dem richtigem Template

21

Page 22: An Introduction to Ruby On Rails

Rails-Architektur

22

Page 23: An Introduction to Ruby On Rails

Active Record

Model

Verbindet Ruby-Klasse mit Daten aus der Datenbank

Bereitstellung von Funktionen zur Manipulation der Daten in der DB

23

Page 24: An Introduction to Ruby On Rails

Action Controller

Controller

Bietet die eigentlichen Funktionalitäten an, mit denen der Benutzer interagiert

Lädt den passenden Action View

24

Page 25: An Introduction to Ruby On Rails

Action View

View

Ruby Template

HTML mit ERb-tags

Das was der Benutzer zu sehen bekommt

25

Page 26: An Introduction to Ruby On Rails

Installation und Einrichtung

26

Page 27: An Introduction to Ruby On Rails

Was brauchen wir?

Ruby

rubygems zum Installieren von gems

Rails & co rubygems

Datenbank

Webserver

27

Page 28: An Introduction to Ruby On Rails

Ruby

Windows Binary Installer:

http://rubyforge.org/projects/rubyinstaller/

UNIX:

RPM, DEB, Sourcen, Port,...

28

Page 29: An Introduction to Ruby On Rails

Rubygems

Paketverwaltung für Bibliotheken (vgl. CPAN und PEAR)

Verwaltung von Paketen

gem install NAME

gem update

u.s.w....

29

Page 30: An Introduction to Ruby On Rails

Rails

Unser Eigentliches Framework

Installation über rubygems

gem install rails

30

Page 31: An Introduction to Ruby On Rails

Datenbank

Rails unterstützt folgende Datenbanken:

MySQL

PostgreSQL

SQLite

Sybase

Oracle

MSSQL31

Page 32: An Introduction to Ruby On Rails

Webserver

Rails kommt mit eigenem Webserver, WEBrick

Ansonsten:

CGI --> Jeder Webserver mit CGI

mod_ruby + Apache

FCGI für Apache oder Lighttpd

32

Page 33: An Introduction to Ruby On Rails

Hello World

33

Page 34: An Introduction to Ruby On Rails

Erste Schritte

Daten-Struktur

Konfiguration und Environments

Kontrollfluß

“Hello World” ausgeben

34

Page 35: An Introduction to Ruby On Rails

Applikation erstellen# rails myapplication

# ls myapplication/

35

Page 36: An Introduction to Ruby On Rails

Datenbank Verbindungconfig/database.yml

36

Page 37: An Introduction to Ruby On Rails

UmgebungenDevelopment

Standard

Loggin an, caching aus, debug an

Production

Loggin an, caching an, debug aus

Test

alles aus 37

Page 38: An Introduction to Ruby On Rails

Generator

Models,Views und Controller können per Hand erstellt werden

Generator hilft aus:

ruby script/generator model Buch

ruby script/generator controller Laden

38

Page 39: An Introduction to Ruby On Rails

Scaffold GeneratorGeneriert Standard Views und Controller für Model:

New

Edit

List

Destroy

Nützlich für den Anfang39

Page 40: An Introduction to Ruby On Rails

Controller generieren# ruby script/generate controller sagwas

40

Page 41: An Introduction to Ruby On Rails

Action einfügen# vi app/controller/sagwas_controler.rb

41

Page 42: An Introduction to Ruby On Rails

Webserver starten# ruby script/server

42

Page 43: An Introduction to Ruby On Rails

Browser starten

http://server/controller/action/[argumente]

43

Page 44: An Introduction to Ruby On Rails

Real World Example

44

OnlineShop in Rails

Page 45: An Introduction to Ruby On Rails

Funktionalität

Admin: Einstellen und Bearbeiten von Artikeln

Store: Ansehen und Warenkorb

Admin: Bestellungen bearbeiten

45

Page 46: An Introduction to Ruby On Rails

Vorgehen - Admin

Erstellen von DB-Tabellen

Generieren von Model+Controller

Validieren eines gültigen Artikels

46

Page 47: An Introduction to Ruby On Rails

# rails depot

# cd depot

# vi config/database.yml

# ruby script/generate scaffold Product Admin

47

Page 48: An Introduction to Ruby On Rails

Validation

ActiveRecord bietet Validierungsfunktionen an, die vor dem Speichern aufgerufen werden:

validate_presence_of :name, :vorname

48

Page 49: An Introduction to Ruby On Rails

Validation

49

Page 50: An Introduction to Ruby On Rails

Anpassen der Templates

Templates sind RHTML, d.h HTML + Ruby code:

<% normaler rubycode %>

<%= ergebnis wird ausgegeben %>

<%=h ergebnis wird HTML escaped %>

50

Page 51: An Introduction to Ruby On Rails

Weitere Features

51

Page 52: An Introduction to Ruby On Rails

The Web, V2.0

Rails integriert JavaScript Bibliotheken, die folgenes ermöglichen:

Ajax

Effects

52

Page 53: An Introduction to Ruby On Rails

The Web, V2.0 - AJAX

53

Page 54: An Introduction to Ruby On Rails

The Web, V2.0 - AJAX

54

Page 55: An Introduction to Ruby On Rails

The Web, V2.0

DEMO

55

Page 56: An Introduction to Ruby On Rails

Remote Debugging

Über ruby script/console bekommt man eine IRB session zur Rails-Umgebung

Das geht auch remote!

56

Page 57: An Introduction to Ruby On Rails

Sicherheit

Schutz gegen CSS/XSS mit <%h .... %>

Schutz gegen SQLInjection mit

User.find(:all, conditions =>

[“name=?”, #{params[:name]}]

57

Page 58: An Introduction to Ruby On Rails

Caching

Rails bietet von sich aus drei Caching Einstellung

Page caching

Action caching

Fragment caching

58

Page 59: An Introduction to Ruby On Rails

ActionMail

Senden und Empfangen von Mail in RailsApplikationen

Senden über Templates

59

Page 60: An Introduction to Ruby On Rails

Ressourcen

60

Page 61: An Introduction to Ruby On Rails

Mailing Listen

[email protected]

[email protected]

61

Page 62: An Introduction to Ruby On Rails

Webseiten

www.rubyonrails.com

poignantguide.net/ruby

mir.aculo.us / script.aculo.us

62

Page 63: An Introduction to Ruby On Rails

Bücher

Programming Ruby

Agile Web Development with Rails

63

Page 64: An Introduction to Ruby On Rails

Fin

64

Fragen?