Download from Wow! eBook <www.wowebook.com>
Learning Rails 3
Simon St.Laurent, Edd Dumbill, and Eric J. Gruber
Beijing • Cambridge • Farnham • Kưln • Sebastopol • Tokyo
Learning Rails 3
by Simon St.Laurent, Edd Dumbill, and Eric J. Gruber
Copyright © 2012 Simon St.Laurent, Edd Dumbill, Eric J. Gruber. All rights reserved.
Printed in the United States of America.
Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.
O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions
are also available for most titles (). For more information, contact our
corporate/institutional sales department: 800-998-9938 or
Editor: Simon St.Laurent
Production Editor: Iris Febres
Proofreader: Jasmine Perez
Indexer: Lucie Haskins
Cover Designer: Karen Montgomery
Interior Designer: David Futato
Illustrators: Robert Romano, Rebecca Demarest,
and Jessamyn Read
July 2012:
First Edition.
Revision History for the First Edition:
2012-07-11
First release
See for release details.
Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of
O’Reilly Media, Inc. Learning Rails 3, the image of the tarpans, and related trade dress are trademarks
of O’Reilly Media, Inc.
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 O’Reilly Media, Inc., was aware of a
trademark claim, the designations have been printed in caps or initial caps.
While every precaution has been taken in the preparation of this book, the publisher and authors assume
no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein.
ISBN: 978-1-449-30933-6
[M]
1342467902
Table of Contents
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi
1. Starting Up Ruby on Rails . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
If You Run Windows, You’re Lucky
Getting Started at the Command Line
Starting Up Rails
Test Your Knowledge
Quiz
Answers
2
3
8
9
9
9
2. Rails on the Web . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Creating Your Own View
What Are All Those Folders?
Adding Some Data
How Hello World Works
Adding Logic to the View
Test Your Knowledge
Quiz
Answers
11
14
16
18
20
22
22
22
3. Adding Web Style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
I Want My CSS!
Specifying Stylesheets
Creating a Layout for a Controller
Choosing a Layout from a Controller
Sharing Template Data with the Layout
Setting a Default Page
Test Your Knowledge
Quiz
Answers
23
28
29
31
33
34
36
36
36
iii
About the Authors
Simon St.Laurent is a web developer, network administrator, computer book author,
and XML troublemaker living in Ithaca, NY. His books include XML: A Primer, XML
Elements of Style, and Building XML Applications, Cookies, and Sharing Bandwidth. He
is a contributing editor to XMLhack.com and an occasional contributor to XML.com.
Edd Dumbill is a technologist, writer, and programmer based in California. He is the
program chair for the O’Reilly Strata and Open Source Convention Conferences.
Eric J. Gruber works as a web developer in municipal government for the City of
Lawrence, KS ( and freelances with his company, Rumblestrut ( />
Colophon
The animals on the cover of Learning Rails 3 are tarpans (Equus ferus ferus). The tarpan
was a wild horse that lived in Europe and Asia and died out in the 19th century. Smaller
and stockier than a modern domestic horse, it was mouse-gray in color with a dark
mane and a black stripe down its back. The breed was known to be intelligent, curious,
and independent.
The ancient tarpan ranged from southern France and Spain to central Russia. Its decline
was caused by the growth of the European human population in the 17th and 18th
centuries, which encroached on the tarpan’s natural habitat. Tarpans were also hunted
for their meat. The last wild tarpan died in Ukraine in 1879, and the last pure tarpan
died in a Russian zoo eight years later, at which point the species officially became
extinct.
However, you can still see a tarpan today, thanks to two German zoologists who succeeded in genetically recreating the breed in the 1930s. Heinz and Lutz Heck began a
breeding program while working at a Munich zoo, believing that genes still present in
the gene pool of an overall species could be used to recreate extinct breeds. They
combined the genes of living horses who showed similar characteristics to the ancient
tarpan, and bred the first modern tarpan at the zoo in 1933. This new form of tarpan,
known as the Heck horse, is a phenotypic copy of the original wild breed, meaning that
it resembles the ancient tarpan but is not exactly the same genetically. Today, there are
about 50 tarpans in North America, all of which trace back to the original project in
Munich. Most of them are owned by private breeders who are trying to increase the
tarpan population. There are not many more than 100 tarpans in the world.
The cover image is from Richard Lydekker’s Royal Natural History. The cover font is
Adobe ITC Garamond. The text font is Linotype Birka; the heading font is Adobe
Myriad Condensed; and the code font is LucasFont’s TheSansMonoCondensed.