Thông tin tài liệu
www.it-ebooks.info
Beta
Book
Agile publishing for agile developers
Under Construction The book you’re reading is still under development. As
part of our Beta book pr ogram, we’re releasing this copy well before a normal
book would be released. That way you’re able to get this content a couple of
months before it’s available in finished form, and we’ll get feedback to make
the book even better. The idea is that everyone wins!
Be warned. The book has not had a full technical edit, so it will contain
errors. It has not been copyedited, so it will be full of typos and other weird-
ness. And there’s been no effort spent doing layout, so you’ll find bad page
breaks, over-long lines with little black rectangles, incorrect hyphenations,
and all the other ugly things that you wouldn’t expect to see in a finished
book. We can’t be held liable if you use this book to try to create a spiffy
application and you somehow end up with a strangely shaped farm imple-
ment instead. Despite all this, we think you’ll enjoy it!
Download Updates Throughout this process you’ll be able to download
updated ebooks from your account on http://pragprog.com. When the book
i
s finally ready, you’ll get the final version (and subsequent updates) from the
same address.
Send us your feedback In the meantime, we’d appreciate you sending us
your feedback on this book at http://pragprog.com/titles/rails4/errata, or by using
t
he links at the bottom of each page.
Thank you for being part of the Pragmatic community!
Andy & Dave
www.it-ebooks.info
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
www.it-ebooks.info
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 ha ve 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 Pr ogra mmers,
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.
C
opyright
©
2010 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.
B11.0 printing, November 24, 2010
Version: 2010-11-24
www.it-ebooks.info
Contents
Changes in the Beta Releases 10
Beta 11—November 24 . . . . . . . . . . . . . . . . . . . . . . . . . 10
Beta 10—October 28 . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Beta 9—October 6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Beta 8—September 9 . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Beta 7—August 25 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Beta 6—July 27 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Beta 5—June 28 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Beta 4—May 26 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Beta 3—May 11 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Beta 2—May 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Preface to the Fourth Edition 15
Acknowledgements 17
Introduction 19
Rails Simply Feels Right . . . . . . . . . . . . . . . . . . . . . . . . . 19
Rails Is Agile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Who This Book Is For . . . . . . . . . . . . . . . . . . . . . . . . . . 22
How To Read This Book . . . . . . . . . . . . . . . . . . . . . . . . . 22
Part I—Getting Started 26
1 Installing Rails 27
1.1 Installing on Windows . . . . . . . . . . . . . . . . . . . . . . 27
1.2 Installing on Mac OS X . . . . . . . . . . . . . . . . . . . . . 29
1.3 Installing on Linux . . . . . . . . . . . . . . . . . . . . . . . . 30
1.4 Choosing a Rails Version . . . . . . . . . . . . . . . . . . . . 31
1.5 Setting Up Your Development Environment . . . . . . . . . 32
1.6 Rails and Databases . . . . . . . . . . . . . . . . . . . . . . . 36
1.7 What We Just Did . . . . . . . . . . . . . . . . . . . . . . . . 37
www.it-ebooks.info
CONTENTS 6
2 Instant Gratification 38
2.1 Creating a New Application . . . . . . . . . . . . . . . . . . . 38
2.2 Hello, Rails! . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
2.3 Linking Pages Together . . . . . . . . . . . . . . . . . . . . . 47
2.4 What We Just Did . . . . . . . . . . . . . . . . . . . . . . . . 49
3 The Architecture of Rails Applications 51
3.1 Models, Views, and Controllers . . . . . . . . . . . . . . . . 51
3.2 Rails Model Support . . . . . . . . . . . . . . . . . . . . . . . 54
3.3 Action Pack: The View and Controller . . . . . . . . . . . . 56
4 Introduction to Ruby 58
4.1 Ruby Is an Object-Oriented Language . . . . . . . . . . . . 58
4.2 Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
4.3 Logic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
4.4 Organizing Structures . . . . . . . . . . . . . . . . . . . . . . 66
4.5 Marshaling Objects . . . . . . . . . . . . . . . . . . . . . . . 69
4.6 Pulling It All Together . . . . . . . . . . . . . . . . . . . . . . 69
4.7 Ruby Idioms . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
Part II—Building an Application 73
5 The Depot Application 74
5.1 Incremental Development . . . . . . . . . . . . . . . . . . . . 74
5.2 What Depot Does . . . . . . . . . . . . . . . . . . . . . . . . . 75
5.3 Let’s Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
6 Task A: Creating the Application 81
6.1 Iteration A1: Creating the Products Maintenance Applica-
t
ion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
6.2 Iteration A2: Making Prettier Listings . . . . . . . . . . . . . 88
7 Task B: Validation and Unit Testing 95
7.1 Iteration B1: Validating! . . . . . . . . . . . . . . . . . . . . . 95
7.2 Iteration B2: Unit Testing of Models . . . . . . . . . . . . . 100
8 Task C: Catalog Display 109
8.1 Iteration C1: Cr eating the Catalog Listing . . . . . . . . . . 109
8.2 Iteration C2: Adding a Page Layout . . . . . . . . . . . . . . 112
8.3 Iteration C3: Using a Helper to Format the Price . . . . . . 116
8.4 Iteration C4: Functional Testing of Controllers . . . . . . . 116
Report erratum
this copy is (B11.0 printing, November 24, 2010)
www.it-ebooks.info
CONTENTS 7
9 Task D: Cart Creation 121
9.1 Iteration D1: Finding a Cart . . . . . . . . . . . . . . . . . . 121
9.2 Iteration D2: Connecting Products to Carts . . . . . . . . . 122
9.3 Iteration D3: Adding a Button . . . . . . . . . . . . . . . . . 124
10 Task E: A Smarter Cart 130
10.1 Iteration E1: Creating a Smarter Cart . . . . . . . . . . . . 130
10.2 Iteration E2: Handling Errors . . . . . . . . . . . . . . . . . 134
10.3 Iteration E3: Finishing the Cart . . . . . . . . . . . . . . . . 138
11 Task F: Add a Dash of Ajax 143
11.1 Iteration F1: Moving the Cart . . . . . . . . . . . . . . . . . 144
11.2 Iteration F2: Creating an Ajax-Based Cart . . . . . . . . . . 149
11.3 Iteration F3: Highlighting Changes . . . . . . . . . . . . . . 152
11.4 Iteration F4: Hiding an Empty Cart . . . . . . . . . . . . . . 154
11.5 Testing Ajax changes . . . . . . . . . . . . . . . . . . . . . . 158
12 Task G: Check Out! 163
12.1 Iteration G1: Capturing an Order . . . . . . . . . . . . . . . 163
12.2 Iteration G2: Atom Feeds . . . . . . . . . . . . . . . . . . . . 177
12.3 Iteration G3: Pagination . . . . . . . . . . . . . . . . . . . . . 181
13 Task H: Sending Mail 186
13.1 Iteration H1: Sending Confirmation E-mails . . . . . . . . . 186
13.2 Iteration H2: Integration Testing of Applications . . . . . . 193
14 Task I: Logging In 199
14.1 Iteration I1: Adding Users . . . . . . . . . . . . . . . . . . . 199
14.2 Iteration I2: Authenticating Users . . . . . . . . . . . . . . . 208
14.3 Iteration I3: Limiting Access . . . . . . . . . . . . . . . . . . 213
14.4 Iteration I4: Adding a Sidebar, More Administration . . . . 216
15 Task J: Internationalization 221
15.1 Iteration J1: Selecting the locale . . . . . . . . . . . . . . . . 222
15.2 Iteration J2: Translating the Store Front . . . . . . . . . . . 224
15.3 Iteration J3: Translating Checkout . . . . . . . . . . . . . . 231
15.4 Iteration J4: Add a Locale Switcher . . . . . . . . . . . . . . 237
16 Task K: Deployment and Production 241
16.1 Iteration K1: Deploying with Phusion Passenger and MySQL 243
16.2 Iteration K2: Deploying Remotely with Capistrano . . . . . 248
16.3 Iteration K3: Checking Up on a Deployed Application . . . 254
Report erratum
this copy is (B11.0 printing, November 24, 2010)
www.it-ebooks.info
CONTENTS 8
17 Depot Retrospective 258
17.1 Rails Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . 258
17.2 Documenting What We Have Done . . . . . . . . . . . . . . 261
Part III—Rails In Depth 263
18 Finding Your Way Around Rails 264
18.1 Where things go . . . . . . . . . . . . . . . . . . . . . . . . . 264
18.2 Naming Conventions . . . . . . . . . . . . . . . . . . . . . . 273
19 Active Record 277
19.1 Defining your Data . . . . . . . . . . . . . . . . . . . . . . . . 277
19.2 Locating and Traversing Records . . . . . . . . . . . . . . . 282
19.3 Creating, Reading, Updating, and Deleting (CRUD) . . . . 285
19.4 Participating in the Monitoring Process . . . . . . . . . . . 301
19.5 Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . 308
20 Action Dispatch and Action Controller 313
20.1 Dispatching Requests to Controllers . . . . . . . . . . . . . 313
20.2 Processing of Requests . . . . . . . . . . . . . . . . . . . . . 324
20.3 Objects and operations that span requests . . . . . . . . . 335
21 Action View 345
21.1 Using Templates . . . . . . . . . . . . . . . . . . . . . . . . . 345
21.2 Generating Forms . . . . . . . . . . . . . . . . . . . . . . . . 347
21.3 Processing Forms . . . . . . . . . . . . . . . . . . . . . . . . 350
21.4 Uploading Files to Rails Applications . . . . . . . . . . . . . 352
21.5 Using Helpers . . . . . . . . . . . . . . . . . . . . . . . . . . . 355
21.6 Reducing Maintenance with Layouts and Partials . . . . . 362
22 Caching 371
22.1 Page Caching . . . . . . . . . . . . . . . . . . . . . . . . . . . 371
22.2 Expiring Pages . . . . . . . . . . . . . . . . . . . . . . . . . . 374
22.3 Fragment Caching . . . . . . . . . . . . . . . . . . . . . . . . 380
23 Migrations 386
23.1 Creating and Running Migrations . . . . . . . . . . . . . . . 386
23.2 Anatomy of a Migration . . . . . . . . . . . . . . . . . . . . . 389
23.3 Managing Tables . . . . . . . . . . . . . . . . . . . . . . . . . 393
23.4 Advanced Migrations . . . . . . . . . . . . . . . . . . . . . . 397
23.5 When Migrations Go Bad . . . . . . . . . . . . . . . . . . . . 401
23.6 Schema Manipulation Outside Migrations . . . . . . . . . . 402
Report erratum
this copy is (B11.0 printing, November 24, 2010)
www.it-ebooks.info
CONTENTS 9
24 Non-Browser Applications 404
24.1 A Standalone Application Using Active Record . . . . . . . 404
24.2 A Library Function Using Active Support . . . . . . . . . . 405
24.3 A Remote Application Using Active Resource . . . . . . . . 410
25 Rails’ Dependencies 416
25.1 Generating XML with Builder . . . . . . . . . . . . . . . . . 416
25.2 Generating HTML with ERb . . . . . . . . . . . . . . . . . . 418
25.3 Managing Dependencies with Bundler . . . . . . . . . . . . 420
25.4 Interfacing with the web server with Rack . . . . . . . . . . 422
25.5 Automating Tasks with Rake . . . . . . . . . . . . . . . . . . 426
25.6 Survey of Rails’ Dependencies . . . . . . . . . . . . . . . . . 427
26 Rails Plugins 431
26.1 Credit Card Processing with Active Merchant . . . . . . . . 431
26.2 Saving Bandwidth with Asset Packager . . . . . . . . . . . 433
26.3 Beautifying our Markup with Haml . . . . . . . . . . . . . . 435
26.4 Write Less and Do More with JQuery . . . . . . . . . . . . . 438
26.5 Finding more at RailsPlugins.org . . . . . . . . . . . . . . . 440
27 Where to Go From Here 443
A Bibliography 445
Index 446
Report erratum
this copy is (B11.0 printing, November 24, 2010)
www.it-ebooks.info
Changes in the Beta Releases
Beta 11—November 24
This beta incorporates a substantial amount of feedback from a number of
sources including errata, formal reviews, and from the wonderful editor of
this book. For the first time since putting this book out in beta, I am going
to suggest that if you are well under way with Depot using a previous beta,
consider keeping on with that beta. While there has been no major changes,
there have been enough minor changes that those that wish to use this book
are encouraged to start over.
This draft has also been tested against the Rails 3.0.3 r elease. No changes
were needed to make the code in this book work against that release.
As always, thanks for all of the wonderful feedback via the errata, forums, and
other venues. At this time I would like to specifically thank Johnathan Ritzi,
David Kapp, and Jason Holloway. If you spot something, it is not too late to
make a comment: there will be at least one more errata sweep before final
printing.
Beta 10—October 28
This beta introduces a chapter on plugins and completes the first draft.
Plug-
ins are not merely an afterthought or an advanced feature of Rails, with Rails
3.0 it is a fully architected way to augment or even replace base Rails func-
tionality.
This also completes the first draft. If you spot something missing, now would
be an excellent time to report it via the forums or via an errata. After a few
weeks of addressing comments it will be onto production where formatting
and typographical and indexing glitches will be resolved.
This draft has also been tested against the Rails 3.0.1 r elease. No changes
were needed to make the code in this book work against that release.
www.it-ebooks.info
[...]... Acknowledgements You’d think that producing a new edition of a book would be easy After all, you already have all the text It’s just a tweak to some code here and a minor wording change there, and you’re done You’d think It’s difficult to tell exactly, but our impression is that creating each edition of Agile Web Development with Rails took about as much effort as the first edition Rails is constantly evolving and,... starting with half of a really good application already in place But there’s something else to Rails—something that’s hard to describe Somehow, it just feels right Of course, you’ll have to take our word for that until you write some Rails applications for yourself (which should be in the next 45 minutes or so ) That’s what this book is all about Rails Is Agile The title of this book is Agile Web Development. .. any suggestions and answers that you may have to questions others may have posted Let’s get started! The first steps are to install Ruby and Rails and to verify the installation with a simple demonstration Agile Web Development with Rails I found it in our local bookstore, and it seemed great!” —Dave’s mum 3 http://forums.pragprog.com/forums/148 4 http://www.pragprog.com/titles/rails4/errata 5 http://www.pragprog.com/wikis/wiki/RailsPlayTime... implementation of a wide range of so-called Web 2.0 applications Why is that? Rails Simply Feels Right First, a large number of developers were frustrated with the technologies they were using to create web applications It didn’t seem to matter whether they were using Java, PHP, or NET—there was a growing sense that their job was just too damn hard And then, suddenly, along came Rails, and Rails was easier But... and my preferences tend toward Vim and Ubuntu, we both share a love for the command line and getting our fingers dirty with code—starting with tangible examples before diving into heavy theory Since the time the third edition was published (and, in fact, since the first, second and third editions), much has changed Rails is in the process of being significantly refactored, mostly internally A number of... along with support for test fixtures and stubs during testing, gives developers the safety net they need when making those changes With a good set of tests in place, changes are less nerve-racking Rather than constantly trying to tie Rails processes to the agile principles, we’ve decided to let the framework speak for itself As you read through the tutorial chapters, try to imagine yourself developing web. .. partially updated to reflect the new direction, but will continue to work with Beta 3 Report erratum this copy is (B11.0 printing, November 24, 2010) 14 www.it-ebooks.info Preface to the Fourth Edition When Dave asked me to join as a coauthor of the third edition of this book, I was thrilled After all, it was from the first printing of the first edition of this book that I had learned Rails Dave and I also have... Doug Rhoten, Gary Sherman, Davanum Srinivas, Stefan Turalski, and José Valim Additionally, each edition of this book has been released as a beta book: early versions were posted as PDFs, and people made comments online And comment they did: more than 800 suggestions and bug reports were posted for this edition alone The vast majority ended up being incorporated, making this book immeasurably more useful... all about Rails Is Agile The title of this book is Agile Web Development with Rails You may be surprised to discover that we don’t have explicit sections on applying agile practices X, Y, and Z to Rails coding The reason is both simple and subtle Agility is part of the fabric of Rails Let’s look at the values expressed in the Agile Manifesto as a set of four preferences:1 • Individuals and interactions... Rails development process isn’t driven by documents You won’t find 500-page specifications at the heart of a Rails project Instead, you’ll find a group of users and developers jointly exploring their need and the possible ways of answering that need You’ll find solutions that change as both the developers and the users become more experienced with the problems they’re trying to solve You’ll find 1 http://agilemanifesto.org/ . & Dave
www.it-ebooks.info
Agile Web Development with Rails
Fourth Edition
Sam Ruby
Dave Thomas
David Heinemeier Hansson
with
Leon Breedt
Mike Clark
James. our impression is that creating each edition
of Agile Web Development with Rails took about as much effort as the first
edition. Rails is constantly evolving
Ngày đăng: 17/02/2014, 08:20
Xem thêm: Tài liệu Agile Web Development with Rails, 4rd Edition pdf, Tài liệu Agile Web Development with Rails, 4rd Edition pdf