Contents 

Ruby on Rails: Up and Running
Table of Contents
Copyright
Preface
Chapter 1. Zero to Sixty: Introducing Rails
Section 1.1. Rails Strengths
Section 1.2. Putting Rails into Action
Section 1.3. Organization
Section 1.4. The Web Server
Section 1.5. Creating a Controller
Section 1.6. Building a View
Section 1.7. Tying the Controller to the View
Section 1.8. Under the Hood
Section 1.9. What's Next?
Chapter 2. Active Record Basics
Section 2.1. Active Record Basics
Section 2.2. Introducing Photo Share
Section 2.3. Schema Migrations
Section 2.4. Basic Active Record Classes
Section 2.5. Attributes
Section 2.6. Complex Classes
Section 2.7. Behavior
Section 2.8. Moving Forward
Chapter 3. Active Record Relationships
Section 3.1. belongs_to
Section 3.2. has_many
Section 3.3. has_one
Section 3.4. What You Haven't Seen
Section 3.5. Looking Ahead
Chapter 4. Scaffolding
Section 4.1. Using the Scaffold Method
Section 4.2. Replacing Scaffolding
Section 4.3. Generating Scaffolding Code
Section 4.4. Moving Forward
Chapter 5. Extending Views
Section 5.1. The Big Picture
Section 5.2. Seeing Real Photos
Section 5.3. View Templates
Section 5.4. Setting the Default Root
Section 5.5. Stylesheets
Section 5.6. Hierarchical Categories
Section 5.7. Styling the Slideshows
Chapter 6. Ajax
Section 6.1. How Rails Implements Ajax
Section 6.2. Playing a Slideshow
Section 6.3. Using Drag-and-Drop to Reorder Slides
Section 6.4. Drag and Drop Everything (Almost Everything)
Section 6.5. Filtering by Category
Chapter 7. Testing
Section 7.1. Background
Section 7.2. Ruby's Test::Unit
Section 7.3. Testing in Rails
Section 7.4. Wrapping Up
Appendix A. Installing Rails
Section 1.1. Windows
Section 2.1. OS X
Section 3.1. Linux
Appendix B. Quick Reference
Section 5.1. General
Section 5.2. Testing
Section 5.3. RJS (Ruby JavaScript)
Section 5.4. Active Record
Section 5.5. Controllers
Section 5.6. Views
Section 5.7. Ajax
Section 5.8. Configuring Your Application
About the Authors
Colophon
Index
A
B
C
D
E
F
G
H
I
J
L
M
N
O
P
R
S
T
U
V
W
X
Y
Z

Ruby on Rails for all.

Prev Page Next Page
Previous Page
Next Page

2.2. Introducing Photo Share

For the remainder of this book, we'll be working on a single application called Photo Share, a database-backed web application that allows users to share photos among acquaintances. We'll start with these simple requirements, called user stories:

  • Let a user view a set of photos on the Web so others can see them.

  • Organize photos in categories.

  • Organize and view slideshows from available photos.

2.2.1. Defining the Model

Rails is a database-centric development environment, so your development will usually begin with the model. You need to determine the types of objects your application will need. A good starting point is to underline the important nouns in a list of user stories. We've used italic to signify important nouns, so we'll have Active Record classes for photos, categories, and slideshows. We'll also need slides, to keep track of the position of each photo in a slideshow.

There are several important relationships:

  • A category has many photos, and a photo can have one or more categories.

  • A category can have other categories.

  • A slideshow has many slides.

  • A slide has one photo.

A simple diagram like the one in Figure 2-1 helps to show the entities and relationships in your model. Index cards work well. For many-to-one relationships, we'll use an arrow to mean belongs to, so the arrow will point from the one to the many. Two-sided arrows are many-to-many, and a line without arrows means one-to-one. We'll represent a tree with an arrow that points back to the originating class. We'll use Active Record to define each of these entities and manage each relationship. Now, let's code them in Active Record.

Figure 2-1. Photos are placed into nested categories and listed in slideshows

2.2.2. Configuring Active Record

As always, we start with a Rails project. First, create a Rails project called photos:

rails photos
cd photos

You've now got a Rails project called photos with three environments: development, test, and production. Rails uses separate databases for each environment (see the sidebar "Three Databases"). To create a database, make sure the MySQL database is started and also start the mysql command prompt:

mysql -u <username> -p <password>

Now create a database called photos_development:

> mysql
...

mysql> create database photos_development;
Query OK, 1 row affected (0.05 sec)

Configure your database. This chapter uses a development database, so you need to edit database.yml to look like this:

development:
  adapter: mysql
  database: photos_development
  username: <your userid>
  password: <your password>
  host: localhost


Previous Page
Next Page