Extracted from: Agile Web Development with Rails 5 This PDF file contains pages extracted from Agile Web Development with Rails 5, published by the Pragmatic Bookshelf. For more information or to purchase a paperback or PDF copy, please visit http://www.pragprog.com. Note: This extract contains some colored text (particularly in code listing). This is available only in online versions of the books. The printed versions are black and white. Pagination might vary between the online and printed versions; the content is otherwise identical. Copyright 2016 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. The Pragmatic Bookshelf Raleigh, North Carolina
Agile Web Development with Rails 5 Sam Ruby Dave Thomas David Heinemeier Hansson The Pragmatic Bookshelf Raleigh, North Carolina
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, PragProg 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 books, screencasts, and audio books can help you and your team create better software and have more fun. Visit us at https://pragprog.com. The team that produced this book includes: Susannah Davidson Pfalzer (editor) Potomac Indexing, LLC (index) Eileen Cohen (copyedit) Gilson Graphics (layout) Janet Furlow (producer) For sales, volume licensing, and support, please contact support@pragprog.com. For international rights, please contact rights@pragprog.com. Copyright 2016 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-13: 978-1-68050-171-1 Encoded using the finest acid-free high-entropy binary digits. Book version: P1.0 September 2016
Contents Foreword to the Rails 5 Edition........? Preface to the Rails 5 Edition.........? Acknowledgments...........? Introduction.............? Part I Getting Started 1. Installing Rails............? Installing on Cloud9? Installing on a Virtual Machine? Installing on Windows? Installing on Mac OS X? Installing on Linux? Choosing a Rails Version? Setting Up Your Development Environment? Rails and Databases? 2. Instant Gratification...........? Creating a New Application? Hello, Rails!? Linking Pages Together? When Things Go Wrong? 3. The Architecture of Rails Applications......? Models, Views, and Controllers? Rails Model Support? Action Pack: The View and Controller? 4. Introduction to Ruby...........? Ruby Is an Object-Oriented Language?
Contents vi Data Types? Logic? Organizing Structures? Marshaling Objects? Pulling It All Together? Ruby Idioms? Part II Building an Application 5. The Depot Application..........? Incremental Development? What Depot Does? Let s Code? 6. Task A: Creating the Application........? Iteration A1: Creating the Product Maintenance Application? Iteration A2: Making Prettier Listings? 7. Task B: Validation and Unit Testing.......? Iteration B1: Validating!? Iteration B2: Unit Testing of Models? 8. Task C: Catalog Display..........? Iteration C1: Creating the Catalog Listing? Iteration C2: Adding a Page Layout? Iteration C3: Using a Helper to Format the Price? Iteration C4: Functional Testing of Controllers? Iteration C5: Caching of Partial Results? 9. Task D: Cart Creation...........? Iteration D1: Finding a Cart? Iteration D2: Connecting Products to Carts? Iteration D3: Adding a Button? 10. Task E: A Smarter Cart..........? Iteration E1: Creating a Smarter Cart? Iteration E2: Handling Errors? Iteration E3: Finishing the Cart? 11. Task F: Add a Dash of Ajax.........? Iteration F1: Moving the Cart? Iteration F2: Creating an Ajax-Based Cart?
Contents vii Iteration F3: Highlighting Changes? Iteration F4: Hiding an Empty Cart? Iteration F5: Broadcasting Updates with Action Cable? 12. Task G: Check Out!...........? Iteration G1: Capturing an Order? Iteration G2: Atom Feeds? 13. Task H: Sending Mail...........? Iteration H1: Sending Confirmation Emails? Iteration H2: Integration Testing of Applications? 14. Task I: Logging In............? Iteration I1: Adding Users? Iteration I2: Authenticating Users? Iteration I3: Limiting Access? Iteration I4: Adding a Sidebar, More Administration? 15. Task J: Internationalization.........? Iteration J1: Selecting the Locale? Iteration J2: Translating the Storefront? Iteration J3: Translating Checkout? Iteration J4: Add a Locale Switcher? 16. Task K: Deployment and Production.......? Iteration K1: Deploying with Phusion Passenger and MySQL? Iteration K2: Deploying Remotely with Capistrano? Iteration K3: Checking Up on a Deployed Application? 17. Depot Retrospective...........? Rails Concepts? Documenting What We ve Done? Part III Rails in Depth 18. Finding Your Way Around Rails........? Where Things Go? Naming Conventions? 19. Active Record.............? Defining Your Data? Locating and Traversing Records?
Contents viii Creating, Reading, Updating, and Deleting (CRUD)? Participating in the Monitoring Process? Transactions? 20. Action Dispatch and Action Controller......? Dispatching Requests to Controllers? Processing of Requests? Objects and Operations That Span Requests? 21. Action View.............? Using Templates? Generating Forms? Processing Forms? Uploading Files to Rails Applications? Using Helpers? Reducing Maintenance with Layouts and Partials? 22. Migrations.............? Creating and Running Migrations? Anatomy of a Migration? Managing Tables? Advanced Migrations? When Migrations Go Bad? Schema Manipulation Outside Migrations? 23. Nonbrowser Applications..........? A Stand-Alone Application Using Active Record? A Library Function Using Active Support? 24. Rails Dependencies...........? Generating XML with Builder? Generating HTML with ERB? Managing Dependencies with Bundler? Interfacing with the Web Server with Rack? Automating Tasks with Rake? Survey of Rails Dependencies? 25. Rails Plugins.............? Credit Card Processing with Active Merchant? Beautifying Our Markup with Haml?
Contents ix Pagination? Finding More at RailsPlugins.org? 26. Where to Go from Here..........? A1. Bibliography.............? Index...............?