ALL SOURCE CODE AVAILABLE FOR DOWNLOAD 'REY�SCALE #-9+ 0ANTONE Black 50% CMYKO,53,100,0 PANTONEOrange021C 0ANTONE TBHUEL LSEITMPPRLOEO AFN WD EEBA SAYP PWLAICY ATTOIO BBlack 100%NCMYK100,45,0,37UPANTONE2955CSILD #-9+ SIM'REY�SCALE PLY R Updated to take advantage of all the new Rails 2 features, Simply Rails 2 is a comprehensive, step-by-step guide to building powerful web applications using A Ruby On Rails. Perfect for the programming novice or someone looking to move into the agile Rails framework, this book will teach you how to build bulletproof Web 2.0 I RAILPANTONEOranSge021C PANT2ONE2955C L CMYKO,53,100,0 CMYK100,45,0,37 applications from scratch, with more features using less code. Black 50% Black 100% S Build and deploy your own Rails web application. “If you’re looking for Reap the benefits of using best-practice MVC architecture. your first Rails book, BY PATRICK LENZ this is a good choice” Use Rails’s Ajax features to create slick interfaces. www.ibm.com Interact with databases easily using ActiveRecord. GARY POLLICE Add the magic of REST to your apps with Rails Resources. “The way this book is Use plugins to enhance your applications easily. laid out is first class” www.rubyinside.com PETER COOPER S ABOUT PATRICK LENZ I Patrick Lenz has been developing web applications for “Definitely a good M more than ten years. Founder and lead developer of the introduction, especially freshmeat.net software portal, he and his Rails consultancy if you are new to Ruby.” P and web application development company, limited weblog.jamisbuck.org L Patrick overload, are responsible for numerous community driven JAMIS BUCK Y web applications developed using Ruby on Rails. R A SITEPOINT BOOKS WEB PROGRAMMING ISBN-13: 978-0-9804552-0-5 I Advocate best practice techniques L Lead you through practical examples S Provide working code for your web site 2 Make learning easy and fun THE ULTIMATE BEGINNER’S GUIDE TO RUBY ON RAILS USD $39.95 CAD $39.95 Visit us on the Web at sitepoint.com orfor sales and support email [email protected] LENZ simplyrails.indd 1 4/16/2008 11:00:53 AM Summary of Contents Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix 1. Introducing Ruby on Rails . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 2. Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 3. Introducing Ruby . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 4. Rails Revealed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 5. Models, Views, and Controllers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 6. Helpers, Forms, and Layouts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 7. Ajax and Web 2.0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197 8. Protective Measures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249 9. Advanced Topics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303 10. Rails Plugins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351 11. Debugging, Testing, and Benchmarking . . . . . . . . . . . . . . . . . . . . . . . . . . . 379 12. Deployment and Production Use . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 425 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 449 SIMPLY RAILS 2 BY PATRICK LENZ SECOND EDITION iv Simply Rails 2 by Patrick Lenz Copyright © 2008 SitePoint Pty. Ltd. Expert Reviewer: Luke Redpath Editor: Hilary Reynolds Managing Editor: Simon Mackie Index Editor: Max McMaster Technical Editor: Andrew Tetlaw Cover Design: Alex Walker Technical Director: Kevin Yank Printing History: First Edition: January 2007 Second Edition: May 2008 Notice of Rights All rights reserved. No part of this book may be reproduced, stored in a retrieval system or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Notice of Liability The author and publisher have made every effort to ensure the accuracy of the information herein. However, the information contained in this book is sold without warranty, either express or implied. Neither the authors and SitePoint Pty. Ltd., nor its dealers or distributors will be held liable for any damages to be caused either directly or indirectly by the instructions contained in this book, or by the software or hardware products described herein. Trademark Notice Rather than indicating every occurrence of a trademarked name as such, this book uses the names only in an editorial fashion and to the benefit of the trademark owner with no intention of infringement of the trademark. Published by SitePoint Pty. Ltd. 48 Cambridge Street Collingwood VIC Australia 3066 Web: www.sitepoint.com Email: [email protected] ISBN 978-0-9804552-0-5 Printed and bound in the United States of America v About the Author Patrick Lenz has been developing web applications for more than ten years. Founder and lead developer of the freshmeat.net software portal, he and his Rails consultancy and web application development company, limited overload, are responsible for several community- driven web applications developed using Ruby on Rails. Patrick also authored some of the first articles to appear on the web about architecting and scaling larger Rails applications. Patrick lives in Wiesbaden, Germany, with his wife Alice and his daughter Gwendolyn. When not working in front of a computer, he can often be seen with a camera in his hand, either taking artsy pictures or documenting the progress of his baby girl conquering the world.1 He also enjoys cars, music, and extended weekend brunches with friends. His weblog can be found at http://poocs.net/. About the Expert Reviewer Luke Redpath is a programmer with over seven years’ experience in the web design and de velopment field. A recovering PHP and ASP developer, Luke has been using Ruby and Rails professionally for nearly two years and has released and contributed to several Ruby libraries and Rails plugins, including UJS—the Rails unobtrusive JavaScript plugin.2 He currently resides in North London with his long-term partner Julie. About the Technical Editor Andrew Tetlaw has been tinkering with web sites as a web developer since 1997 and has also worked as a high school English teacher, an English teacher in Japan, a window cleaner, a car washer, a kitchen hand, and a furniture salesman. At SitePoint he is dedicated to making the world a better place through the technical editing of SitePoint books and kits. He is also a busy father of five, enjoys coffee, and often neglects his blog at http://tetlaw.id.au/. About the Technical Director As Technical Director for SitePoint, Kevin Yank oversees all of its technical publica tions—books, articles, newsletters, and blogs. He has written over 50 articles for SitePoint, but is best known for his book, Build Your Own Database Driven Website Using PHP & 1 His pictures are regularly published to Flickr and are available at http://flickr.com/photos/scoop/ 2 http://www.ujs4rails.com/ vi MySQL. 3 Kevin lives in Melbourne, Australia, and enjoys performing improvised comedy theater and flying light aircraft. About SitePoint SitePoint specializes in publishing fun, practical, and easy-to-understand content for web professionals. Visit http://www.sitepoint.com/ to access our books, newsletters, articles, and community forums. 3 http://www.sitepoint.com/books/phpmysql1/ To my daughter Gwendolyn and my wife Alice. Table of Contents Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix Who Should Read This Book? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix What You’ll Learn . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xx What’s in This Book? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xx The Book’s Web Site . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxii The Code Archive . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxii Updates and Errata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii The SitePoint Forums . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii The SitePoint Newsletters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii Your Feedback . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii Conventions Used in This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiv Code Samples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiv Tips, Notes, and Warnings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv Chapter 1 Introducing Ruby on Rails . . . . . . . . . . . . . 1 History . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Development Principles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Convention Over Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Don’t Repeat Yourself . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Agile Development . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Building the Example Web Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 What Is Digg? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Features of the Example Application . . . . . . . . . . . . . . . . . . . . . . . . . 12 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14