Page 1
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
Install Ruby and Rails
• Ruby - http://www.ruby-lang.org/en/downloads/
• Rails -
© 2008 Barry Burd 2
Page 3
Install SQLite
• http://www.sqlite.org/download.html
© 2008 Barry Burd 3
Copy the uncompressed files to your ruby\bin directory
Page 4
Install SQLite (continued)
© 2008 Barry Burd 4
Page 5
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
Install the
RadRails plugin
within Aptana
© 2008 Barry Burd 6
Page 7
Configure RadRails
© 2008 Barry Burd 7
Page 8
Configure RadRails
© 2008 Barry Burd 8
Page 9
Configure RadRails
© 2008 Barry Burd 9
Page 10
The Aptana IDE
• Based on Eclipse
– View
– Editor
– Part
– Area
– Perspective
© 2008 Barry Burd 10
Page 11
Ruby
Pers
© 2008 Barry Burd11
spective
Page 12
Rails
Per
© 2008 Barry Burd 12
rspective
Page 13
Data
Per
© 2008 Barry Burd 13
rspective
Page 14
Debug
Per
© 2008 Barry Burd 14
rspective
Page 15
Watch a movie...
http://www.burdbrain.com/RubyOnRails/Rails2.wmv
© 2008 Barry Burd 15
Page 16
RadRails Perspective
© 2008 Barry Burd 16
Page 17
Create a new Rails Project
© 2008 Barry Burd 17
Page 18
Name the project
© 2008 Barry Burd18
Page 19
Rails creates
folders and files
© 2008 Barry Burd 19
Page 20
Create a database
© 2008 Barry Burd 20
Page 21
Generate a scaffold
© 2008 Barry Burd 21
Page 22
Migrate the databaseCreate table(s) and column(s)
© 2008 Barry Burd 22
Page 23
Start the project’s Web server
© 2008 Barry Burd 23
Page 24
Visit the Web server’s Welcome page
© 2008 Barry Burd 24
Page 25
Visit the “list” page
© 2008 Barry Burd 25
Page 26
Add a new record
© 2008 Barry Burd 26
Page 27
© 2008 Barry Burd 27
Page 28
Show an existing record
© 2008 Barry Burd 28
Page 29
Edit an existing record
© 2008 Barry Burd 29
Page 30
Several types
of fields
© 2008 Barry Burd30
Page 31
The Migration FileBookstore\db\migrate\001_create_books.rb
© 2008 Barry Burd 31
Page 32
Bookstore\config\database.yml
© 2008 Barry Burd 32
Page 33
The Model CodeBookstore\app\models\book.rb
© 2008 Barry Burd 33
Page 34
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
The Controller CodeBookstore\app\controllers\books_controller.rb
© 2008 Barry Burd 35
Page 36
The Controller CodeBookstore\app\controllers\books_controller.rb
© 2008 Barry Burd 36
Page 37
The Controller CodeBookstore\app\controllers\books_controller.rb
© 2008 Barry Burd
Page 38
The Index PageBookstore\app\views\books\index.html.erb
© 2008 Barry Burd
View Source
Page 39
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
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
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
The Edit PageBookstore\app\views\books\edit.html.erb
© 2008 Barry Burd 42
Page 43
The New PageBookstore\app\views\books\new.html.erb
© 2008 Barry Burd 43
Page 44
The Show PageBookstore\app\views\books\show.html.erb
© 2008 Barry Burd 44
Page 45
Fine tuning a Web application
© 2008 Barry Burd 45
Page 46
Adding validation to the model code
© 2008 Barry Burd 46
Page 47
Attempting to create a new record, but...
© 2008 Barry Burd 47
Page 48
...validation errors
© 2008 Barry Burd 48
Page 49
The view’s code takes
notice of errors
© 2008 Barry Burd 49
Page 50
Rails has several validates methods
50© 2008 Barry Burd
Page 51
Relationships between tables
© 2008 Barry Burd 51
Page 52
Bookstore\db\migrate\002_create_reviews.rb
© 2008 Barry Burd 52
Page 53
Run the most recent migration
© 2008 Barry Burd 53
Page 54
Bookstore\app\models\book.rb
© 2008 Barry Burd 54
Page 55
Bookstore\app\models\review.rb
© 2008 Barry Burd 55
Page 56
The Rails Console
© 2008 Barry Burd 56
Page 57
© 2008 Barry Burd 57
Page 58
Add code toBookstore\app\views\books\show.html.erb
© 2008 Barry Burd 58
Page 59
Add a method toBookstore\app\controllers\books_controller.rb
© 2008 Barry Burd 59
Page 60
© 2008 Barry Burd 60
Page 61
© 2008 Barry Burd 61