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