Agile Web Development With Rails, 2nd Edition (2006).pdf

(6083 KB) Pobierz
Agile Web Development with Rails
204285345.015.png
Important Information
About Rails Versions
Rails is an evolving framework. The core Rails developers are continually
making changes, adding new features, fixing bugs, and so on. Periodically
they package up the latest version of Rails into a release. These releases are
then available to application developers as RubyGems.
This book is written for Rails 1.2.
As the book is going to press the core team have created the codebase for
Rails 1.2. However, they have not yet packaged it into a gem. This gives us a
bit of a problem. We want the book to reflect all the latest and greatest Rails
features, but we also know that it is hard for folks to jump through the hoops
required to get the so-called Edge version of Rails installed on their systems.
And until a gem is available, the 1.2 features are only available in Edge Rails.
Now, it may well be that by the time you get your hands on this book, the
Rails 1.2 gem is out. It’s easy to find out. After you’ve installed Rails (as
described in Chapter 3 , Installing Rails, on page 31 ) , bring up a command
prompt and enter rails -v . If it reports “Rails 1.2” or later, you’re fine.
If instead you see something like “Rails 1.1.6,” you’ll need to update to get
the code in this book to run. We’ve prepared a snapshot of the Rails frame-
work code that we used when writing this book. You can install it in your
own Rails applications as a temporary measure until 1.2 is released.
• Create your application normally. You’ll find that it will contain a direc-
tory called vendor
application’s vendor directory and unzip it. It should create a new direc-
tory called rails
• In your application’s top-level directory, issue the command
rake rails:update
Once Rails 1.2 is released, you can install it and remove the directory tree
vendor/rails from your applications.
The version of Rails from our web site is not an official release, and should
not be used in production applications.
Dave Thomas
204285345.016.png 204285345.017.png 204285345.018.png 204285345.001.png
Agile Web Development with Rails
Second Edition
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
Copyright © 2006 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: 0-9776166-3-0
ISBN-13: 978-0-9776166-3-3
Printed on acid-free paper with 85% recycled, 30% post-consumer content.
P1.00 printing, November 22, 2006
Version: 2006-11-22
204285345.002.png 204285345.003.png 204285345.004.png 204285345.005.png 204285345.006.png 204285345.007.png 204285345.008.png 204285345.009.png 204285345.010.png 204285345.011.png 204285345.012.png 204285345.013.png 204285345.014.png
Contents
12
14
1.1
Rails Is Agile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
1.2
Finding Your Way Around . . . . . . . . . . . . . . . . . . . . . 17
1.3
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . 19
21
22
2.1
Models, Views, and Controllers . . . . . . . . . . . . . . . . . . 22
2.2
Active Record: Rails Model Support . . . . . . . . . . . . . . . . 25
2.3
Action Pack: The View and Controller . . . . . . . . . . . . . . 29
31
3.1
Your Shopping List . . . . . . . . . . . . . . . . . . . . . . . . . 31
3.2
Installing on Windows . . . . . . . . . . . . . . . . . . . . . . . . 32
3.3
Installing on Mac OS X . . . . . . . . . . . . . . . . . . . . . . . 34
3.4
Installing on Linux . . . . . . . . . . . . . . . . . . . . . . . . . . 35
3.5
Development Environments . . . . . . . . . . . . . . . . . . . . 36
3.6
Rails and Databases . . . . . . . . . . . . . . . . . . . . . . . . . 39
3.7
Keeping Up-to-Date . . . . . . . . . . . . . . . . . . . . . . . . . 42
3.8
Rails and ISPs . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
43
4.1
Creating a New Application . . . . . . . . . . . . . . . . . . . . . 43
4.2
Hello, Rails! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
4.3
Linking Pages Together . . . . . . . . . . . . . . . . . . . . . . . 56
4.4
What We Just Did . . . . . . . . . . . . . . . . . . . . . . . . . . 59
 
Zgłoś jeśli naruszono regulamin