Prepared exclusively for Mathieu Beautru
Agile Web Development with Rails
A Pragmatic Guide
Dave Thomas
David Heinemeier Hansson
with
Leon Breedt
Mike Clark
Thomas Fuchs
Andreas Schwarz
The Pragmatic Bookshelf
Raleigh, North Carolina Dallas, Texas
Prepared exclusively for Mathieu Beautru
Bookshelf
Pragmatic
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
Programmers, 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 Programming, 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.pragmaticprogrammer.com
Copyright
©
2005 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 0-9766940-0-X
Printed on acid-free paper with 85% recycled, 30% post-consumer content.
Fourth printing, December 2005
Version: 2005-12-24
Prepared exclusively for Mathieu Beautru
Contents
1Introduction 1
1.1 Rails Is Agile ........................... 3
1.2 Finding Your Way Around ................... 4
1.3 Acknowledgments ........................ 6
Part I—Getting Started 8
2 The Architecture of Rails Applications 9
2.1 Models, Views, and Controllers ................ 9
2.2 Active Record: Rails Model Support ............. 13
2.3 Action Pack: The View and Controller ............ 17
3 Installing Rails 19
3.1 Installing on Windows ..................... 19
3.2 Installing on Mac OS X ..................... 20
3.3 Installing on Unix/Linux .................... 20
3.4 Rails and Databases ...................... 21
3.5 Keeping Up-to-Date ....................... 24
3.6 Rails and ISPs .......................... 24
4 Instant Gratification 25
4.1 Creating a New Application .................. 25
4.2 Hello, Rails! ........................... 27
4.3 Linking Pages Together ..................... 37
4.4 What We Just Did ........................ 41
Prepared exclusively for Mathieu Beautru
CONTENTS v
Part II—Building an Application 42
5 The Depot Application 43
5.1 Incremental Development ................... 43
5.2 What Depot Does ........................ 44
5.3 Let’s Code ............................ 48
6 Task A: Product Maintenance 49
6.1 Iteration A1: Get Something Running ............ 49
6.2 Iteration A2: Add a Missing Column ............. 57
6.3 Iteration A3: Validate! ..................... 60
6.4 Iteration A4: Prettier Listings ................. 63
7 Task B: Catalog Display 67
7.1 Iteration B1: Create the Catalog Listing ........... 67
7.2 Iteration B2: Add Page Decorations ............. 70
8 Task C: Cart Creation 74
8.1 Sessions ............................. 74
8.2 More Tables, More Models ................... 76
8.3 Iteration C1: Creating a Cart ................. 78
8.4 Iteration C2: Handling Errors ................. 86
8.5 Iteration C3: Finishing the Cart ................ 90
9 Task D: Checkout! 95
9.1 Iteration D1: Capturing an Order ............... 96
9.2 Iteration D2: Show Cart Contents on Checkout ...... 104
10 Task E: Shipping 109
10.1 Iteration E1: Basic Shipping ................. 109
11 Task F: Administrivia 118
11.1 Iteration F1: Adding Users ................... 118
11.2 Iteration F2: Logging In .................... 123
11.3 Iteration F3: Limiting Access ................. 125
11.4 Finishing Up ........................... 129
11.5 More Icing on the Cake ..................... 130
Report erratum
Prepared exclusively for Mathieu Beautru