Agile Web Development with Rails Does an Excellent Job of Making the Rails Environment Accessible in an Enjoyable and Memorable Way
Total Page:16
File Type:pdf, Size:1020Kb
Important Information About Rails Versions This book is written for Rails 3. The Rails core team is continuing to work on Rails. From time to time, new releases may introduce incompatibilities for applications written for prior versions of Rails, including the code in this book. To run the examples provided in this book, it is important that you install the correct version of Rails, as described in Chapter 1, Installing Rails, on page 24 To determine the version of Rails that you are running, you can issue rails -v at a command prompt. Information on changes to Rails that affect this book can be found at http:// www.pragprog.com/wikis/wiki/ChangesToRails. Sam, Dave, and David When I started learning Ruby on Rails, I read the first edition of this book. Its holistic view of the Rails framework and community provides any new developer the kick start they need to a highly successful career. After reading through the latest edition cover to cover, I can happily say that it continues that trend and remains the first book I recommend to any new Rails devel- oper. Mikel Lindsaar Rails core commit team, creator of the Ruby Mail library, and director, RubyX Agile Web Development with Rails does an excellent job of making the Rails environment accessible in an enjoyable and memorable way. In addition, this book is the first I’ve seen that provides a sensible and coherent explanation of the MVC pattern, and it does so in a natural progression using examples that completely remove any mystery. Ken Coar Author, open software evangelist, and Apache developer Agile Web Development with Rails successfully straddles a fine line between being a fun-to-read introduction to Rails (and Ruby) and a straightforward guide to some advanced features of the platform, nicely supplanting the ever- changing online documentation. Glen Daniels Independent technologist and consultant I’ve never read a programming book as successful as Agile Web Development with Rails. Sam made learning Ruby on Rails easy, comprehensive, and fun. Keith Ballinger Chairman of WS-I’s first Basic Profile working group; author; and key contributor to the .NET and Visual Studio .NET frameworks Agile Web Development with Rails Fourth Edition Sam Ruby Dave Thomas David Heinemeier Hansson with Leon Breedt Mike Clark James Duncan Davidson Justin Gehtland Andreas Schwarz The Pragmatic Bookshelf Raleigh, North Carolina Dallas, Texas Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and The Pragmatic Program- mers, LLC was aware of a trademark claim, the designations have been printed in initial capital letters or in all capitals. The Pragmatic Starter Kit, The Pragmatic Programmer, Pragmatic Program- ming, Pragmatic Bookshelf and the linking g device are trademarks of The Pragmatic Programmers, LLC. Every precaution was taken in the preparation of this book. However, the publisher assumes no responsibility for errors or omissions, or for damages that may result from the use of information (including program listings) contained herein. Our Pragmatic courses, workshops, and other products can help you and your team create better software and have more fun. For more information, as well as the latest Pragmatic titles, please visit us at http://www.pragprog.com. Copyright © 2011 The Pragmatic Programmers LLC. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form, or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior consent of the publisher. Printed in the United States of America. ISBN-10: 1-934356-54-9 ISBN-13: 978-1-934356-54-8 Printed on acid-free paper. P1.0 printing, March 2011 Version: 2011-3-29 Contents Preface to the Fourth Edition 12 Acknowledgments 14 Introduction 16 Rails Simply Feels Right ......................... 16 Rails Is Agile ............................... 18 Who This Book Is For .......................... 19 How To Read This Book ......................... 19 Part I—Getting Started 23 1 Installing Rails 24 1.1 Installing on Windows ...................... 24 1.2 Installing on Mac OS X ..................... 26 1.3 Installing on Linux ........................ 27 1.4 Choosing a Rails Version .................... 28 1.5 Setting Up Your Development Environment . 29 1.6 Rails and Databases ....................... 33 1.7 What We Just Did ........................ 34 2 Instant Gratification 35 2.1 Creating a New Application ................... 35 2.2 Hello, Rails! ............................ 37 2.3 Linking Pages Together ..................... 44 2.4 What We Just Did ........................ 47 3 The Architecture of Rails Applications 48 3.1 Models, Views, and Controllers . 48 3.2 Rails Model Support ....................... 51 3.3 Action Pack: The View and Controller . 53 CONTENTS 8 4 Introduction to Ruby 55 4.1 Ruby Is an Object-Oriented Language . 55 4.2 Data Types ............................ 57 4.3 Logic ................................ 60 4.4 Organizing Structures ...................... 63 4.5 Marshaling Objects ....................... 66 4.6 Pulling It All Together ...................... 66 4.7 Ruby Idioms ........................... 67 Part II—Building an Application 70 5 The Depot Application 71 5.1 Incremental Development .................... 71 5.2 What Depot Does ......................... 72 5.3 Let’s Code ............................. 76 6 Task A: Creating the Application 77 6.1 Iteration A1: Creating the Products Maintenance Applica- tion ................................. 77 6.2 Iteration A2: Making Prettier Listings . 83 7 Task B: Validation and Unit Testing 90 7.1 Iteration B1: Validating! ..................... 90 7.2 Iteration B2: Unit Testing of Models . 95 8 Task C: Catalog Display 105 8.1 Iteration C1: Creating the Catalog Listing . 105 8.2 Iteration C2: Adding a Page Layout . 108 8.3 Iteration C3: Using a Helper to Format the Price . 112 8.4 Iteration C4: Functional Testing of Controllers . 113 9 Task D: Cart Creation 117 9.1 Iteration D1: Finding a Cart . 117 9.2 Iteration D2: Connecting Products to Carts . 118 9.3 Iteration D3: Adding a Button . 120 10 Task E: A Smarter Cart 126 10.1 Iteration E1: Creating a Smarter Cart . 126 10.2 Iteration E2: Handling Errors . 131 10.3 Iteration E3: Finishing the Cart . 134 Report erratum this copy is (P1.0 printing, March 2011) CONTENTS 9 11 Task F: Add a Dash of Ajax 139 11.1 Iteration F1: Moving the Cart . 140 11.2 Iteration F2: Creating an Ajax-Based Cart . 145 11.3 Iteration F3: Highlighting Changes . 148 11.4 Iteration F4: Hiding an Empty Cart . 150 11.5 Testing Ajax Changes . 154 12 Task G: Check Out! 158 12.1 Iteration G1: Capturing an Order . 158 12.2 Iteration G2: Atom Feeds . 172 12.3 Iteration G3: Pagination . 176 13 Task H: Sending Mail 181 13.1 Iteration H1: Sending Confirmation Emails . 181 13.2 Iteration H2: Integration Testing of Applications . 188 14 Task I: Logging In 194 14.1 Iteration I1: Adding Users . 194 14.2 Iteration I2: Authenticating Users . 203 14.3 Iteration I3: Limiting Access . 208 14.4 Iteration I4: Adding a Sidebar, More Administration . 211 15 Task J: Internationalization 216 15.1 Iteration J1: Selecting the Locale . 217 15.2 Iteration J2: Translating the Storefront . 219 15.3 Iteration J3: Translating Checkout . 226 15.4 Iteration J4: Add a Locale Switcher . 232 16 Task K: Deployment and Production 235 16.1 Iteration K1: Deploying with Phusion Passenger and MySQL 237 16.2 Iteration K2: Deploying Remotely with Capistrano . 242 16.3 Iteration K3: Checking Up on a Deployed Application . 248 17 Depot Retrospective 252 17.1 Rails Concepts . 252 17.2 Documenting What We Have Done . 256 Report erratum this copy is (P1.0 printing, March 2011) CONTENTS 10 Part III—Rails in Depth 257 18 Finding Your Way Around Rails 258 18.1 Where Things Go . 258 18.2 Naming Conventions . 267 19 Active Record 271 19.1 Defining Your Data . 271 19.2 Locating and Traversing Records . 276 19.3 Creating, Reading, Updating, and Deleting (CRUD) . 279 19.4 Participating in the Monitoring Process . 295 19.5 Transactions . 302 20 Action Dispatch and Action Controller 307 20.1 Dispatching Requests to Controllers . 307 20.2 Processing of Requests . 318 20.3 Objects and Operations That Span Requests . 329 21 Action View 339 21.1 Using Templates . 339 21.2 Generating Forms . 341 21.3 Processing Forms . 344 21.4 Uploading Files to Rails Applications . 345 21.5 Using Helpers . 349 21.6 Reducing Maintenance with Layouts and Partials . 355 22 Caching 364 22.1 Page Caching . 364 22.2 Expiring Pages . 367 22.3 Fragment Caching . 373 23 Migrations 379 23.1 Creating and Running Migrations . 379 23.2 Anatomy of a Migration . 382 23.3 Managing Tables . 386 23.4 Advanced Migrations . 390 23.5 When Migrations Go Bad . 394 23.6 Schema Manipulation Outside Migrations . 395 24 Nonbrowser Applications 396 24.1 A Stand-Alone Application Using Active Record . 396 24.2 A Library Function Using Active Support . 397 24.3 A Remote Application Using Active Resource . 402 Report erratum this copy is (P1.0 printing, March 2011) CONTENTS 11 25 Rails’ Dependencies 408 25.1 Generating XML with Builder . 408 25.2 Generating HTML with ERb . 410 25.3 Managing Dependencies with Bundler . 412 25.4 Interfacing with the Web Server with Rack . 414 25.5 Automating Tasks with Rake . 418 25.6 Survey of Rails’ Dependencies . 419 26 Rails Plugins 423 26.1 Credit Card Processing with Active Merchant . 423 26.2 Saving Bandwidth with Asset Packager . 425 26.3 Beautifying Our Markup with Haml . 427 26.4 Write Less and Do More with JQuery . 430 26.5 Finding More at RailsPlugins.org . 432 27 Where to Go from Here 435 A Bibliography 437 Index 438 Report erratum this copy is (P1.0 printing, March 2011) Preface to the Fourth Edition When Dave asked me to join as a coauthor of the third edition of this book, I was thrilled.