- •Introduction
- •Rails Is Agile
- •Finding Your Way Around
- •Acknowledgments
- •Getting Started
- •Models, Views, and Controllers
- •Installing Rails
- •Installing on Windows
- •Installing on Mac OS X
- •Installing on Unix/Linux
- •Rails and Databases
- •Keeping Up-to-Date
- •Rails and ISPs
- •Creating a New Application
- •Hello, Rails!
- •Linking Pages Together
- •What We Just Did
- •Building an Application
- •The Depot Application
- •Incremental Development
- •What Depot Does
- •Task A: Product Maintenance
- •Iteration A1: Get Something Running
- •Iteration A2: Add a Missing Column
- •Iteration A4: Prettier Listings
- •Task B: Catalog Display
- •Iteration B1: Create the Catalog Listing
- •Iteration B2: Add Page Decorations
- •Task C: Cart Creation
- •Sessions
- •More Tables, More Models
- •Iteration C1: Creating a Cart
- •Iteration C3: Finishing the Cart
- •Task D: Checkout!
- •Iteration D2: Show Cart Contents on Checkout
- •Task E: Shipping
- •Iteration E1: Basic Shipping
- •Task F: Administrivia
- •Iteration F1: Adding Users
- •Iteration F2: Logging In
- •Iteration F3: Limiting Access
- •Finishing Up
- •More Icing on the Cake
- •Task T: Testing
- •Tests Baked Right In
- •Testing Models
- •Testing Controllers
- •Using Mock Objects
- •Test-Driven Development
- •Running Tests with Rake
- •Performance Testing
- •The Rails Framework
- •Rails in Depth
- •Directory Structure
- •Naming Conventions
- •Active Support
- •Logging in Rails
- •Debugging Hints
- •Active Record Basics
- •Tables and Classes
- •Primary Keys and IDs
- •Connecting to the Database
- •Relationships between Tables
- •Transactions
- •More Active Record
- •Acts As
- •Aggregation
- •Single Table Inheritance
- •Validation
- •Callbacks
- •Advanced Attributes
- •Miscellany
- •Action Controller and Rails
- •Context and Dependencies
- •The Basics
- •Routing Requests
- •Action Methods
- •Caching, Part One
- •The Problem with GET Requests
- •Action View
- •Templates
- •Builder templates
- •RHTML Templates
- •Helpers
- •Formatting Helpers
- •Linking to Other Pages and Resources
- •Pagination
- •Form Helpers
- •Layouts and Components
- •Adding New Templating Systems
- •Introducing AJAX
- •The Rails Way
- •Advanced Techniques
- •Action Mailer
- •Sending E-mail
- •Receiving E-mail
- •Testing E-mail
- •Web Services on Rails
- •Dispatching Modes
- •Using Alternate Dispatching
- •Method Invocation Interception
- •Testing Web Services
- •Protocol Clients
- •Securing Your Rails Application
- •SQL Injection
- •Cross-Site Scripting (CSS/XSS)
- •Avoid Session Fixation Attacks
- •Creating Records Directly from Form Parameters
- •Knowing That It Works
- •Deployment and Scaling
- •Picking a Production Platform
- •A Trinity of Environments
- •Iterating in the Wild
- •Maintenance
- •Finding and Dealing with Bottlenecks
- •Case Studies: Rails Running Daily
- •Appendices
- •Introduction to Ruby
- •Ruby Names
- •Regular Expressions
- •Source Code
- •Cross-Reference of Code Samples
- •Resources
- •Index
ACKNOWLEDGMENTS 6
refresher, you might want to go read Appendix A, on page 467 before
you go too much further. There’s a lot of code in this book....
David Says...
Every now and then you’ll come across a David Says... sidebar. Here’s where David Heinemeier Hansson gives you the real scoop on some particular aspect of Rails—rationales, tricks, recommendations, and more. As he’s the fellow who invented Rails, these are the sections to read if you want to become a Rails pro.
Joe Asks...
Joe, the mythical developer, sometimes pops up to ask questions about stuff we talk about in the text. We try to answer these as we go along.
This book isn’t a reference manual for Rails. We show most of the modules and most of their methods, either by example or narratively in the text, but we don’t have hundreds of pages of API listings. There’s a good reason for this—you get that documentation whenever you install Rails, and it’s guaranteed to be more up-to-date than the material in this book. If you install Rails using RubyGems (which we recommend), simply start the Gem documentation server (using the command gem_server) and you can access all the Rails APIs by pointing your browser at http://localhost:8808.
Rails Versions
This book documents Rails V1.0, which became available in mid 2005. However, as the first printing went to press in June 2005, this magic milestone had not yet been reached. In order to be timely, the APIs described in this book are those for Rails 1.0. The code in the book has been tested against the 0.13 release of Rails, the last release before Rails 1.0.
1.3 Acknowledgments
This book turned out to be a massive undertaking. It would never have happened without an enormous amount of help from the Ruby and the Rails communities. It’s hard to list everyone who contributed, so if you helped out but your name doesn’t appear here, please know that it’s a simple oversight.
This book had an incredible group of reviewers—between them, they generated over 6 megabytes of comments. So, heartfelt thanks to
Prepared exclusively for Rida Al Barazi
Report erratum
ACKNOWLEDGMENTS 7
Alan Francis, Amy Hoy, Andreas Schwarz, Ben Galbraith, Bill Katz, Carl Dearmin, Chad Fowler, Curt Micol, David Rupp, David Vincelli, Dion Almaer, Duane Johnson, Erik Hatcher, Glenn Vanderburg, Gunther Schmidl, Henri ter Steeg, James Duncan Davidson, Johannes Brodwall, John Harechmak, John Johnson, Justin Forder, Justin Gehtland, Kim Shrier, Krishna Dole, Leon Breedt,
Marcel Molina Jr., Michael Koziarski, Mike Clark, Miles K. Forrest, Raymond Brigleb, Robert Rasmussen, Ryan Lowe, Sam Stephenson, Scott Barron, Stefan Arentz, Steven Baker, Stian Grytøyr,
Tait Stevens, Thomas Fuchs, Tom Moertel, and Will Schenk.
Rails was evolving as the book was coming together. As a result, the good folks in the Rails core team spent many hours answering Dave’s questions and generally sympathizing. (They also spent many hours tormenting me by changing stuff I’d just documented, but we won’t go into that here.) A big thank you to
Jamis Buck (minam), Jeremy Kemper (bitsweat),
Marcel Molina Jr, (noradio), Nicholas Seckar (Ulysses),
Sam Stephenson (sam), Scott Barron (htonl),
Thomas Fuchs (madrobby), and Tobias Lütke (xal).
Nathan Colgate Clark responded to a plea on the Rails mailing list and produced the wonderful image we use for the David Says... boxes.
Justin Forder did a great job of fixing up Dave’s anemic style sheets for the Depot application.
Thousands of people participated in the beta program for this book. Thank you all for taking the chance. Hundreds of these people took time to enter comments and errata on what they read. This book is better for it.
Last, but by no means least, we’d like to thank the folks who contributed the specialized chapters to the book: Leon Breedt, Mike Clark, Thomas Fuchs, and Andreas Schwarz.
From Dave Thomas
My family hasn’t seen me for the last eight months. For their patience, support, and love, I’m forever grateful. Thank you Juliet, Zachary, and Henry.
From David Heinemeier Hansson
Marianne: For the patience of endless late nights hacking on Rails.
Prepared exclusively for Rida Al Barazi
Report erratum