Top Banner
Rails 2.0 1 Barry Burd [email protected] This slide presentation © 2008 Barry Burd Copyrights for Ruby, Rails and other material are held by their respective owners.
61

Rails 2 - Drew University for Ruby, Rails and other material are held by their respective owners. Install Ruby and Rails • Ruby -

Jun 10, 2018

Download

Documents

VănDũng
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Rails 2 - Drew University for Ruby, Rails and other material are held by their respective owners. Install Ruby and Rails • Ruby -

Rails 2.0

1

Barry Burd

[email protected]

This slide presentation © 2008 Barry Burd

Copyrights for Ruby, Rails and other material are held by their

respective owners.

Page 2: Rails 2 - Drew University for Ruby, Rails and other material are held by their respective owners. Install Ruby and Rails • Ruby -

Install Ruby and Rails

• Ruby - http://www.ruby-lang.org/en/downloads/

• Rails -

© 2008 Barry Burd 2

Page 3: Rails 2 - Drew University for Ruby, Rails and other material are held by their respective owners. Install Ruby and Rails • Ruby -

Install SQLite

• http://www.sqlite.org/download.html

© 2008 Barry Burd 3

Copy the uncompressed files to your ruby\bin directory

Page 4: Rails 2 - Drew University for Ruby, Rails and other material are held by their respective owners. Install Ruby and Rails • Ruby -

Install SQLite (continued)

© 2008 Barry Burd 4

Page 5: Rails 2 - Drew University for Ruby, Rails and other material are held by their respective owners. Install Ruby and Rails • Ruby -

Install the Aptana IDE

• You need the Java Runtime -

http://java.sun.com/javase/downloads/index.jsp

• Aptana - http://www.aptana.com/download/index.php

© 2008 Barry Burd 5

Page 6: Rails 2 - Drew University for Ruby, Rails and other material are held by their respective owners. Install Ruby and Rails • Ruby -

Install the

RadRails plugin

within Aptana

© 2008 Barry Burd 6

Page 7: Rails 2 - Drew University for Ruby, Rails and other material are held by their respective owners. Install Ruby and Rails • Ruby -

Configure RadRails

© 2008 Barry Burd 7

Page 8: Rails 2 - Drew University for Ruby, Rails and other material are held by their respective owners. Install Ruby and Rails • Ruby -

Configure RadRails

© 2008 Barry Burd 8

Page 9: Rails 2 - Drew University for Ruby, Rails and other material are held by their respective owners. Install Ruby and Rails • Ruby -

Configure RadRails

© 2008 Barry Burd 9

Page 10: Rails 2 - Drew University for Ruby, Rails and other material are held by their respective owners. Install Ruby and Rails • Ruby -

The Aptana IDE

• Based on Eclipse

– View

– Editor

– Part

– Area

– Perspective

© 2008 Barry Burd 10

Page 11: Rails 2 - Drew University for Ruby, Rails and other material are held by their respective owners. Install Ruby and Rails • Ruby -

Ruby

Pers

© 2008 Barry Burd11

spective

Page 12: Rails 2 - Drew University for Ruby, Rails and other material are held by their respective owners. Install Ruby and Rails • Ruby -

Rails

Per

© 2008 Barry Burd 12

rspective

Page 13: Rails 2 - Drew University for Ruby, Rails and other material are held by their respective owners. Install Ruby and Rails • Ruby -

Data

Per

© 2008 Barry Burd 13

rspective

Page 14: Rails 2 - Drew University for Ruby, Rails and other material are held by their respective owners. Install Ruby and Rails • Ruby -

Debug

Per

© 2008 Barry Burd 14

rspective

Page 15: Rails 2 - Drew University for Ruby, Rails and other material are held by their respective owners. Install Ruby and Rails • Ruby -

Watch a movie...

http://www.burdbrain.com/RubyOnRails/Rails2.wmv

© 2008 Barry Burd 15

Page 16: Rails 2 - Drew University for Ruby, Rails and other material are held by their respective owners. Install Ruby and Rails • Ruby -

RadRails Perspective

© 2008 Barry Burd 16

Page 17: Rails 2 - Drew University for Ruby, Rails and other material are held by their respective owners. Install Ruby and Rails • Ruby -

Create a new Rails Project

© 2008 Barry Burd 17

Page 18: Rails 2 - Drew University for Ruby, Rails and other material are held by their respective owners. Install Ruby and Rails • Ruby -

Name the project

© 2008 Barry Burd18

Page 19: Rails 2 - Drew University for Ruby, Rails and other material are held by their respective owners. Install Ruby and Rails • Ruby -

Rails creates

folders and files

© 2008 Barry Burd 19

Page 20: Rails 2 - Drew University for Ruby, Rails and other material are held by their respective owners. Install Ruby and Rails • Ruby -

Create a database

© 2008 Barry Burd 20

Page 21: Rails 2 - Drew University for Ruby, Rails and other material are held by their respective owners. Install Ruby and Rails • Ruby -

Generate a scaffold

© 2008 Barry Burd 21

Page 22: Rails 2 - Drew University for Ruby, Rails and other material are held by their respective owners. Install Ruby and Rails • Ruby -

Migrate the databaseCreate table(s) and column(s)

© 2008 Barry Burd 22

Page 23: Rails 2 - Drew University for Ruby, Rails and other material are held by their respective owners. Install Ruby and Rails • Ruby -

Start the project’s Web server

© 2008 Barry Burd 23

Page 24: Rails 2 - Drew University for Ruby, Rails and other material are held by their respective owners. Install Ruby and Rails • Ruby -

Visit the Web server’s Welcome page

© 2008 Barry Burd 24

Page 25: Rails 2 - Drew University for Ruby, Rails and other material are held by their respective owners. Install Ruby and Rails • Ruby -

Visit the “list” page

© 2008 Barry Burd 25

Page 26: Rails 2 - Drew University for Ruby, Rails and other material are held by their respective owners. Install Ruby and Rails • Ruby -

Add a new record

© 2008 Barry Burd 26

Page 27: Rails 2 - Drew University for Ruby, Rails and other material are held by their respective owners. Install Ruby and Rails • Ruby -

© 2008 Barry Burd 27

Page 28: Rails 2 - Drew University for Ruby, Rails and other material are held by their respective owners. Install Ruby and Rails • Ruby -

Show an existing record

© 2008 Barry Burd 28

Page 29: Rails 2 - Drew University for Ruby, Rails and other material are held by their respective owners. Install Ruby and Rails • Ruby -

Edit an existing record

© 2008 Barry Burd 29

Page 30: Rails 2 - Drew University for Ruby, Rails and other material are held by their respective owners. Install Ruby and Rails • Ruby -

Several types

of fields

© 2008 Barry Burd30

Page 31: Rails 2 - Drew University for Ruby, Rails and other material are held by their respective owners. Install Ruby and Rails • Ruby -

The Migration FileBookstore\db\migrate\001_create_books.rb

© 2008 Barry Burd 31

Page 32: Rails 2 - Drew University for Ruby, Rails and other material are held by their respective owners. Install Ruby and Rails • Ruby -

Bookstore\config\database.yml

© 2008 Barry Burd 32

Page 33: Rails 2 - Drew University for Ruby, Rails and other material are held by their respective owners. Install Ruby and Rails • Ruby -

The Model CodeBookstore\app\models\book.rb

© 2008 Barry Burd 33

Page 34: Rails 2 - Drew University for Ruby, Rails and other material are held by their respective owners. Install Ruby and Rails • Ruby -

The Controller CodeBookstore\app\controllers\books_controller.rb

"if the client wants HTML in

response to this action, just

respond as we would have

before, but if the client wants

XML, return them the list of

people in XML format." (Rails

determines the desired

response format from the

HTTP Accept header submitted

© 2008 Barry Burd 34

HTTP Accept header submitted

by the client.)”

Let the instance variable (the attribute) @book be

the record you get when

you look for a record

whose id matches the

incoming id parameter.

Page 35: Rails 2 - Drew University for Ruby, Rails and other material are held by their respective owners. Install Ruby and Rails • Ruby -

The Controller CodeBookstore\app\controllers\books_controller.rb

© 2008 Barry Burd 35

Page 36: Rails 2 - Drew University for Ruby, Rails and other material are held by their respective owners. Install Ruby and Rails • Ruby -

The Controller CodeBookstore\app\controllers\books_controller.rb

© 2008 Barry Burd 36

Page 37: Rails 2 - Drew University for Ruby, Rails and other material are held by their respective owners. Install Ruby and Rails • Ruby -

The Controller CodeBookstore\app\controllers\books_controller.rb

© 2008 Barry Burd

Page 38: Rails 2 - Drew University for Ruby, Rails and other material are held by their respective owners. Install Ruby and Rails • Ruby -

The Index PageBookstore\app\views\books\index.html.erb

© 2008 Barry Burd

View Source

Page 39: Rails 2 - Drew University for Ruby, Rails and other material are held by their respective owners. Install Ruby and Rails • Ruby -

View Source

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">xml:lang="en" lang="en">

<head>

<meta http-equiv="content-type" content="text/html;charset=UTF-8" />

<title>Books: index</title>

<link href="/stylesheets/scaffold.css?1201328122" media="screen" rel="stylesheet" type="text/css" />

</head>

© 2008 Barry Burd 39

Page 40: Rails 2 - Drew University for Ruby, Rails and other material are held by their respective owners. Install Ruby and Rails • Ruby -

View Source

<body>

<p style="color: green"></p>

<h1>Listing books</h1>

<table>

<tr>

<th>Title</th>

<th>Isbn</th><th>Isbn</th>

</tr>

<tr>

<td>Ruby on Rails For Dummies</td>

<td>12233355566622444</td>

<td><a href="/books/1">Show</a></td>

<td><a href="/books/1/edit">Edit</a></td>

<td><a href="/books/1" onclick="if (confirm('Are you sure?')) { var f = ...(some code omitted) 40

Page 41: Rails 2 - Drew University for Ruby, Rails and other material are held by their respective owners. Install Ruby and Rails • Ruby -

View Source

<body>

<p style="color: green"></p>

<h1>Listing books</h1>

<table>

<tr>

<th>Title</th>

<th>Isbn</th><th>Isbn</th>

</tr>

<tr>

<td>Ruby on Rails For Dummies</td>

<td>12233355566622444</td>

<td><a href="/books/1">Show</a></td>

<td><a href="/books/1/edit">Edit</a></td>

<td><a href="/books/1" onclick="if (confirm('Are you sure?')) { var f = ...(remaining code omitted)41

Page 42: Rails 2 - Drew University for Ruby, Rails and other material are held by their respective owners. Install Ruby and Rails • Ruby -

The Edit PageBookstore\app\views\books\edit.html.erb

© 2008 Barry Burd 42

Page 43: Rails 2 - Drew University for Ruby, Rails and other material are held by their respective owners. Install Ruby and Rails • Ruby -

The New PageBookstore\app\views\books\new.html.erb

© 2008 Barry Burd 43

Page 44: Rails 2 - Drew University for Ruby, Rails and other material are held by their respective owners. Install Ruby and Rails • Ruby -

The Show PageBookstore\app\views\books\show.html.erb

© 2008 Barry Burd 44

Page 45: Rails 2 - Drew University for Ruby, Rails and other material are held by their respective owners. Install Ruby and Rails • Ruby -

Fine tuning a Web application

© 2008 Barry Burd 45

Page 46: Rails 2 - Drew University for Ruby, Rails and other material are held by their respective owners. Install Ruby and Rails • Ruby -

Adding validation to the model code

© 2008 Barry Burd 46

Page 47: Rails 2 - Drew University for Ruby, Rails and other material are held by their respective owners. Install Ruby and Rails • Ruby -

Attempting to create a new record, but...

© 2008 Barry Burd 47

Page 48: Rails 2 - Drew University for Ruby, Rails and other material are held by their respective owners. Install Ruby and Rails • Ruby -

...validation errors

© 2008 Barry Burd 48

Page 49: Rails 2 - Drew University for Ruby, Rails and other material are held by their respective owners. Install Ruby and Rails • Ruby -

The view’s code takes

notice of errors

© 2008 Barry Burd 49

Page 50: Rails 2 - Drew University for Ruby, Rails and other material are held by their respective owners. Install Ruby and Rails • Ruby -

Rails has several validates methods

50© 2008 Barry Burd

Page 51: Rails 2 - Drew University for Ruby, Rails and other material are held by their respective owners. Install Ruby and Rails • Ruby -

Relationships between tables

© 2008 Barry Burd 51

Page 52: Rails 2 - Drew University for Ruby, Rails and other material are held by their respective owners. Install Ruby and Rails • Ruby -

Bookstore\db\migrate\002_create_reviews.rb

© 2008 Barry Burd 52

Page 53: Rails 2 - Drew University for Ruby, Rails and other material are held by their respective owners. Install Ruby and Rails • Ruby -

Run the most recent migration

© 2008 Barry Burd 53

Page 54: Rails 2 - Drew University for Ruby, Rails and other material are held by their respective owners. Install Ruby and Rails • Ruby -

Bookstore\app\models\book.rb

© 2008 Barry Burd 54

Page 55: Rails 2 - Drew University for Ruby, Rails and other material are held by their respective owners. Install Ruby and Rails • Ruby -

Bookstore\app\models\review.rb

© 2008 Barry Burd 55

Page 56: Rails 2 - Drew University for Ruby, Rails and other material are held by their respective owners. Install Ruby and Rails • Ruby -

The Rails Console

© 2008 Barry Burd 56

Page 57: Rails 2 - Drew University for Ruby, Rails and other material are held by their respective owners. Install Ruby and Rails • Ruby -

© 2008 Barry Burd 57

Page 58: Rails 2 - Drew University for Ruby, Rails and other material are held by their respective owners. Install Ruby and Rails • Ruby -

Add code toBookstore\app\views\books\show.html.erb

© 2008 Barry Burd 58

Page 59: Rails 2 - Drew University for Ruby, Rails and other material are held by their respective owners. Install Ruby and Rails • Ruby -

Add a method toBookstore\app\controllers\books_controller.rb

© 2008 Barry Burd 59

Page 60: Rails 2 - Drew University for Ruby, Rails and other material are held by their respective owners. Install Ruby and Rails • Ruby -

© 2008 Barry Burd 60

Page 61: Rails 2 - Drew University for Ruby, Rails and other material are held by their respective owners. Install Ruby and Rails • Ruby -

© 2008 Barry Burd 61