Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Agile Web Development With Rails, 2nd Edition (2006).pdf
Скачиваний:
30
Добавлен:
17.08.2013
Размер:
6.23 Mб
Скачать

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 framework 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 directory called vendor

Download http://media.pragprog.com/titles/rails2/code/rails.zip into your application’s vendor directory and unzip it. It should create a new directory 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

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 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 © 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

 

 

 

Contents

 

 

Preface to the Second Edition

12

1

Introduction

14

 

1.1

Rails Is Agile . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . 16

 

1.2

Finding Your Way Around . . . . . . . . . . . . . .

. . . . . . . 17

 

1.3

Acknowledgments . . . . . . . . . . . . . . . . . . .

. . . . . . . 19

Part I—Getting Started

21

2 The Architecture of Rails Applications

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

3

Installing Rails

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

4

Instant Gratification

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

CONTENTS

6

Part II—Building an Application

61

5

The Depot Application

62

 

5.1

Incremental Development . . . . . . . . . . . . . . . . . . . . .

62

 

5.2

What Depot Does . . . . . . . . . . . . . . . . . . . . . . . . . .

63

 

5.3

Let’s Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

67

6

Task A: Product Maintenance

68

 

6.1

Iteration A1: Get Something Running . . . . . . . . . . . . . .

68

 

6.2

Iteration A2: Add a Missing Column . . . . . . . . . . . . . . .

79

 

6.3

Iteration A3: Validate! . . . . . . . . . . . . . . . . . . . . . . . .

81

 

6.4

Iteration A4: Prettier Listings . . . . . . . . . . . . . . . . . . .

85

7

Task B: Catalog Display

94

 

7.1

Iteration B1: Create the Catalog Listing . . . . . . . . . . . . .

94

 

7.2

Iteration B2: Add a Page Layout . . . . . . . . . . . . . . . . . .

98

 

7.3

Iteration B3: Use a Helper to Format the Price . . . . . . . . .

100

 

7.4

Iteration B4: Linking to the Cart . . . . . . . . . . . . . . . . .

100

8

Task C: Cart Creation

104

8.1 Sessions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104 8.2 Iteration C1: Creating a Cart . . . . . . . . . . . . . . . . . . . . 107 8.3 Iteration C2: A Smarter Cart . . . . . . . . . . . . . . . . . . . . 110 8.4 Iteration C3: Handling Errors . . . . . . . . . . . . . . . . . . . 113 8.5 Iteration C4: Finishing the Cart . . . . . . . . . . . . . . . . . . 118

9 Task D: Add a Dash of AJAX

122

9.1 Iteration D1: Moving the Cart . . . . . . . . . . . . . . . . . . . 123 9.2 Iteration D2: An AJAX-Based Cart . . . . . . . . . . . . . . . . 128 9.3 Iteration D3: Highlighting Changes . . . . . . . . . . . . . . . . 131 9.4 Iteration D4: Hide an Empty Cart . . . . . . . . . . . . . . . . . 133 9.5 Iteration D5: Degrading If Javascript Is Disabled . . . . . . . . 137 9.6 What We Just Did . . . . . . . . . . . . . . . . . . . . . . . . . . 138

10

Task E: Check Out!

140

 

10.1

Iteration E1: Capturing an Order . . . . . . . . . . . . . . . . .

140

11

Task F: Administration

155

 

11.1

Iteration F1: Adding Users . . . . . . . . . . . . . . . . . . . . .

155

 

11.2

Iteration F2: Logging In . . . . . . . . . . . . . . . . . . . . . . .

163

 

11.3

Iteration F3: Limiting Access . . . . . . . . . . . . . . . . . . . .

166

 

11.4

Iteration F4: A Sidebar, More Administration . . . . . . . . . .

168

Report erratum

 

 

CONTENTS

7

12 Task G: One Last Wafer-Thin Change

174

 

12.1

Generating the XML Feed . . . . . . . . . . . . . . . . . . . . . .

174

 

12.2

Finishing Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

181

 

13 Task T: Testing

184

 

13.1

Tests Baked Right In . . . . . . . . . . . . . . . . . . . . . . . .

184

 

13.2

Unit Testing of Models . . . . . . . . . . . . . . . . . . . . . . .

185

 

13.3

Functional Testing of Controllers . . . . . . . . . . . . . . . . .

197

 

13.4

Integration Testing of Applications . . . . . . . . . . . . . . . .

212

 

13.5

Performance Testing . . . . . . . . . . . . . . . . . . . . . . . . .

220

 

13.6

Using Mock Objects . . . . . . . . . . . . . . . . . . . . . . . . .

224

 

Part III—The Rails Framework

227

14 Rails in Depth

228

14.1

So, Where’s Rails? . . . . . . . . . . . . . . . . . . . . . . . . . .

228

14.2

Directory Structure . . . . . . . . . . . . . . . . . . . . . . . . .

228

14.3

Rails Configuration . . . . . . . . . . . . . . . . . . . . . . . . .

237

14.4

Naming Conventions . . . . . . . . . . . . . . . . . . . . . . . .

240

14.5

Logging in Rails . . . . . . . . . . . . . . . . . . . . . . . . . . .

243

14.6

Debugging Hints . . . . . . . . . . . . . . . . . . . . . . . . . . .

244

14.7

What’s Next . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

246

15 Active Support

247

15.1

Generally Available Extensions . . . . . . . . . . . . . . . . . .

247

15.2

Enumerations and Arrays . . . . . . . . . . . . . . . . . . . . .

248

15.3

String Extensions . . . . . . . . . . . . . . . . . . . . . . . . . .

249

15.4

Extensions to Numbers . . . . . . . . . . . . . . . . . . . . . . .

251

15.5

Time and Date Extensions . . . . . . . . . . . . . . . . . . . . .

252

15.6

An Extension to Ruby Symbols . . . . . . . . . . . . . . . . . .

254

15.7

with_options . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

255

15.8

Unicode Support . . . . . . . . . . . . . . . . . . . . . . . . . . .

256

16 Migrations

262

16.1

Creating and Running Migrations . . . . . . . . . . . . . . . . .

263

16.2

Anatomy of a Migration . . . . . . . . . . . . . . . . . . . . . . .

265

16.3

Managing Tables . . . . . . . . . . . . . . . . . . . . . . . . . . .

269

16.4

Data Migrations . . . . . . . . . . . . . . . . . . . . . . . . . . .

274

16.5

Advanced Migrations . . . . . . . . . . . . . . . . . . . . . . . .

277

16.6

When Migrations Go Bad . . . . . . . . . . . . . . . . . . . . . .

279

16.7

Schema Manipulation Outside Migrations . . . . . . . . . . . .

280

16.8

Managing Migrations . . . . . . . . . . . . . . . . . . . . . . . .

281

Report erratum

 

 

CONTENTS

8

17 Active Record: The Basics

283

 

17.1

Tables and Classes . . . . . . . . . . . . . . . . . . . . . . . . .

284

 

17.2

Columns and Attributes . . . . . . . . . . . . . . . . . . . . . .

284

 

17.3

Primary Keys and IDs . . . . . . . . . . . . . . . . . . . . . . . .

288

 

17.4

Connecting to the Database . . . . . . . . . . . . . . . . . . . .

290

 

17.5

CRUD—Create, Read, Update, Delete . . . . . . . . . . . . . . .

295

 

17.6

Aggregation and Structured Data . . . . . . . . . . . . . . . . .

313

 

17.7

Miscellany . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

320

 

18 Active Record: Relationships between Tables

323

 

18.1

Creating Foreign Keys . . . . . . . . . . . . . . . . . . . . . . . .

324

 

18.2

Specifying Relationships in Models . . . . . . . . . . . . . . . .

326

 

18.3

belongs_to and has_xxx Declarations . . . . . . . . . . . . . . .

328

 

18.4

Joining to Multiple Tables . . . . . . . . . . . . . . . . . . . . .

343

 

18.5

Self-referential Joins . . . . . . . . . . . . . . . . . . . . . . . .

353

 

18.6

Acts As . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

354

 

18.7

When Things Get Saved . . . . . . . . . . . . . . . . . . . . . . .

358

 

18.8

Preloading Child Rows . . . . . . . . . . . . . . . . . . . . . . .

360

 

18.9

Counters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

361

 

19 Active Record: Object Life Cycle

363

 

19.1 Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363

19.2 Callbacks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373

19.3 Advanced Attributes . . . . . . . . . . . . . . . . . . . . . . . . . 380

19.4 Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 383

20 Action Controller: Routing and URLs

392

20.1

The Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

392

20.2

Routing Requests . . . . . . . . . . . . . . . . . . . . . . . . . .

393

21 Action Controller and Rails

424

21.1

Action Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . .

424

21.2

Cookies and Sessions . . . . . . . . . . . . . . . . . . . . . . . .

435

21.3

Flash—Communicating between Actions . . . . . . . . . . . .

445

21.4

Filters and Verification . . . . . . . . . . . . . . . . . . . . . . .

447

21.5

Caching, Part One . . . . . . . . . . . . . . . . . . . . . . . . . .

455

21.6

The Problem with GET Requests . . . . . . . . . . . . . . . . .

462

Report erratum

 

 

CONTENTS

9

22 Action View

465

 

22.1

Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

465

 

22.2

Using Helpers . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

471

 

22.3

Helpers for Formatting, Linking, and Pagination . . . . . . . .

473

 

22.4

How Forms Work . . . . . . . . . . . . . . . . . . . . . . . . . . .

480

 

22.5

Forms That Wrap Model Objects . . . . . . . . . . . . . . . . .

482

 

22.6

Custom Form Builders . . . . . . . . . . . . . . . . . . . . . . .

494

 

22.7

Working with Nonmodel Fields . . . . . . . . . . . . . . . . . . .

498

 

22.8

Uploading Files to Rails Applications . . . . . . . . . . . . . . .

501

 

22.9

Layouts and Components . . . . . . . . . . . . . . . . . . . . .

505

 

22.10

Caching, Part Two . . . . . . . . . . . . . . . . . . . . . . . . . .

513

 

22.11

Adding New Templating Systems . . . . . . . . . . . . . . . . .

518

 

23 The Web, V2.0

521

 

23.1

Prototype . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

521

 

23.2

Script.aculo.us . . . . . . . . . . . . . . . . . . . . . . . . . . . .

542

 

23.3

RJS Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . .

558

 

23.4

Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

565

 

24 Action Mailer

567

 

24.1

Sending E-mail . . . . . . . . . . . . . . . . . . . . . . . . . . . .

567

 

24.2

Receiving E-mail . . . . . . . . . . . . . . . . . . . . . . . . . . .

578

 

24.3

Testing E-mail . . . . . . . . . . . . . . . . . . . . . . . . . . . .

579

 

25 Web Services on Rails

583

 

25.1

What AWS Is (and What It Isn’t) . . . . . . . . . . . . . . . . . .

583

 

25.2

The API Definition . . . . . . . . . . . . . . . . . . . . . . . . . .

584

 

25.3

Dispatching Modes . . . . . . . . . . . . . . . . . . . . . . . . .

589

 

25.4

Using Alternate Dispatching . . . . . . . . . . . . . . . . . . . .

590

 

25.5

Method Invocation Interception . . . . . . . . . . . . . . . . . .

592

 

25.6

Testing Web Services . . . . . . . . . . . . . . . . . . . . . . . .

594

 

25.7

Protocol Clients . . . . . . . . . . . . . . . . . . . . . . . . . . .

597

 

Part IV—Secure and Deploy Your Application

598

26 Securing Your Rails Application

599

26.1

SQL Injection . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

599

26.2

Creating Records Directly from Form Parameters . . . . . . .

601

26.3

Don’t Trust ID Parameters . . . . . . . . . . . . . . . . . . . . .

603

26.4

Don’t Expose Controller Methods . . . . . . . . . . . . . . . . .

604

26.5

Cross-Site Scripting (CSS/XSS) . . . . . . . . . . . . . . . . . .

605

26.6

Avoid Session Fixation Attacks . . . . . . . . . . . . . . . . . .

607

26.7

File Uploads . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

608

Report erratum

 

 

CONTENTS

10

26.8

Don’t Store Sensitive Information in the Clear . . . . . . . . .

609

 

26.9

Use SSL to Transmit Sensitive Information . . . . . . . . . . .

610

 

26.10

Don’t Cache Authenticated Pages . . . . . . . . . . . . . . . . .

611

 

26.11

Knowing That It Works . . . . . . . . . . . . . . . . . . . . . . .

611

 

27 Deployment and Production

613

 

27.1

Starting Early . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

614

 

27.2

How a Production Server Works . . . . . . . . . . . . . . . . . .

615

 

27.3

Comparing Front-End Web Servers . . . . . . . . . . . . . . . .

617

 

27.4

Repeatable Deployments with Capistrano . . . . . . . . . . . .

618

 

27.5

Setting Up a Deployment Environment . . . . . . . . . . . . . .

619

 

27.6

Checking Up on a Deployed Application . . . . . . . . . . . . .

625

 

27.7

Production Application Chores . . . . . . . . . . . . . . . . . .

626

 

27.8

Moving On to Launch and Beyond . . . . . . . . . . . . . . . .

627

 

Part V—Appendices

629

A

Introduction to Ruby

630

 

A.1

Ruby Is an Object-Oriented Language . . . . . . . . . . . . . .

630

 

A.2

Ruby Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

631

 

A.3

Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

632

 

A.4

Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

634

 

A.5

Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

636

 

A.6

Arrays and Hashes . . . . . . . . . . . . . . . . . . . . . . . . .

637

 

A.7

Control Structures . . . . . . . . . . . . . . . . . . . . . . . . . .

638

 

A.8

Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . .

639

 

A.9

Blocks and Iterators . . . . . . . . . . . . . . . . . . . . . . . . .

639

 

A.10

Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

640

 

A.11

Marshaling Objects . . . . . . . . . . . . . . . . . . . . . . . . .

641

 

A.12

Interactive Ruby . . . . . . . . . . . . . . . . . . . . . . . . . . .

641

 

A.13

Ruby Idioms . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

641

 

A.14

RDoc Documentation . . . . . . . . . . . . . . . . . . . . . . . .

643

B

Configuration Parameters

644

 

B.1

Top-Level Configuration . . . . . . . . . . . . . . . . . . . . . .

644

 

B.2

Active Record Configuration . . . . . . . . . . . . . . . . . . . .

646

 

B.3

Action Controller Configuration . . . . . . . . . . . . . . . . . .

648

 

B.4

Action View Configuration . . . . . . . . . . . . . . . . . . . . .

649

 

B.5

Action Mailer Configuration . . . . . . . . . . . . . . . . . . . .

650

 

B.6

Test Case Configuration . . . . . . . . . . . . . . . . . . . . . .

651

Report erratum

 

 

 

CONTENTS

11

C

Source Code

652

 

 

C.1 The Full Depot Application . . . . . . . . . . . . . . . . . . . . .

652

 

D

Resources

683

 

 

D.1

Online Resources . . . . . . . . . . . . . . . . . . . . . . . . . .

683

 

 

D.2

Bibliography . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

683

 

 

Index

 

684

 

Report erratum