Prepared exclusively for Vanilton
Important Information
About Rails Versions
This book is written for Rails 2. As this printing of the book is going to press,
the current gener ally available Gem version of Rails is 2.1. The code i n this
book has been tested against this version.
This book started life with the same text as the Second Edition, which cov-
ered Rails 1 .2.6. Pages containing this original text have a gray heading and
footer. As w e migrate content to Rails 2, you’ll find the hea d er col or changes
to red. The Third Edition preface contains a table summarizing the status
of this update process. In particular, note t hat in many of the chapters the
screenshots have not yet been updated to match the results produced by
Rails 2 .1.
The R ails core team is continuing to work on Rails 2. From time to time, new
releases may introduce incompatibilities for applications written for pr ior
versions of Rails. In ord er to experiment with these changes, the Rails d evel-
opers are making the changes ava ilable via Edge Rails (discussed starting on
page
244). These cha nges won’t affect you unless y ou expl icitly instal l this
experimental code—you won’t find yourself running it unless you overide
Gem defau lts or deliberately choose to use Edge Rails. However, if you do
decide to run this experimental Rails code, you’ll find that some stuff in this
book ( and some stuff in your existing Rails applications) may no longer run.
To determine the version of Rails that you are running, you can issue
rails -v
at a command prompt.
Sam, Dave, and David
Prepared exclusively for Vanilton
Agile Web De velopment with Rails
Third Edition
Sam Ruby
Dave Thomas
David Heinemeier Hansson
with
Leon Breedt
Mike Clark
James Duncan Davidson
Justin Gehtland
Andr eas Schwarz
The Pragmatic Bookshelf
Raleigh, North Carolina Dallas, Texas
Prepared exclusively for Vanilton
Many of the designations used b y 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 Book shelf and the linking g device are trademarks of The Pragm atic Programmers,
LLC.
Every precaution was taken in the preparation of this book. However, the publisher assumes no
responsibility for errors or omissions, or for dam ages that may result f rom the use of information
(including program listings) contained herein.
Our Pragmat ic courses, workshops, and other products can help you and your team create bett er
software and have more fun. For more information, as well as the latest Pragmatic titles, please
visit us at
http://www.pragprog.com
Copyright
©
2008 The Pragmatic Programmers LLC.
All rights reserved.
No part of this pub lication 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 Unit ed States of America.
ISBN-10: 1-934356-16-6
ISBN-13: 978-1-9343561-6-6
Printed on acid-free paper with 50% recycled, 15% post-consumer content.
B1.3 printing, June 4, 2008
Version: 2008-6-4
Prepared exclusively for Vanilton
Con t ents
Preface to the Seco nd Edition 12
Preface to the Third Edition 14
1 Introduction 16
1.1 Rails Is Agile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
1.2 Finding Your Way Around . . . . . . . . . . . . . . . . . . . . . 19
1.3 Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Part I—Getting Started 23
2 The Architecture of Rails Applications 24
2.1 Models, Views, and Controllers . . . . . . . . . . . . . . . . . . 24
2.2 Active Record: Rails Model Support . . . . . . . . . . . . . . . . 27
2.3 Action Pack: The View and Controller . . . . . . . . . . . . . . . 31
3 Installing Rails 33
3.1 Your Shopping List . . . . . . . . . . . . . . . . . . . . . . . . . . 33
3.2 Installing on Windows . . . . . . . . . . . . . . . . . . . . . . . . 33
3.3 Installing on Mac OS X . . . . . . . . . . . . . . . . . . . . . . . 35
3.4 Installing on Linux . . . . . . . . . . . . . . . . . . . . . . . . . . 36
3.5 Development E nvironments . . . . . . . . . . . . . . . . . . . . 37
3.6 Rails and Databases . . . . . . . . . . . . . . . . . . . . . . . . . 41
3.7 Keeping Up-to-Date . . . . . . . . . . . . . . . . . . . . . . . . . 42
3.8 Rails and ISPs . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
4 Instant Gratification 44
4.1 Creating a New Application . . . . . . . . . . . . . . . . . . . . . 44
4.2 Hello, Rails! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
4.3 Linking Pages Together . . . . . . . . . . . . . . . . . . . . . . . 57
4.4 What We Just Did . . . . . . . . . . . . . . . . . . . . . . . . . . 60
Prepared exclusively for Vanilton