Early praise for Agile Web Development with Rails 5.1 The best book to get started in the Rails world. A comprehensive, coherent, and concise overview of the Ruby on Rails framework. It treats learning in a gradual way, creating an application from scratch using the latest technologies. ➤ Luis Miguel Cabezas Granado Ruby on Rails and PHP developer at Junta de Extremadura (Spain) and PHP book writer at Anaya Multimedia, I liked how the book guided me through each step of the tasks. This book gives a thorough introduction to Rails, and I’d suggest it to anyone who wants to start development with Rails. ➤ Gábor László Hajba Software Developer, EBCONT Enterprise Technologies The book was really pleasant to read; I liked how it creates a foundational under- standing of Rails with a realistic scenario and then builds upon it for the more advanced topics. ➤ Alessandro Bahgat Software Engineer, Google We've left this page blank to make the page numbers the same in the electronic and paper books. We tried just leaving it out, but then people wrote us to ask about the missing pages. Anyway, Eddy the Gerbil wanted to say “hello.” Agile Web Development with Rails 5.1 Sam Ruby David Bryant Copeland with Dave Thomas 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 trade- marks 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: Publisher: Andy Hunt VP of Operations: Janet Furlow Development Editor: Susannah Davidson Pfalzer Indexing: Potomac Indexing, LLC Copy Editor: Molly McBeath Layout: Gilson Graphics For sales, volume licensing, and support, please contact [email protected]. For international rights, please contact [email protected]. Copyright © 2017 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-251-0 Encoded using the finest acid-free high-entropy binary digits. Book version: P1.0—November 2017 Contents Foreword to the Rails 5 Edition . . . . . . . . xi Preface to the Rails 5.1 Edition . . . . . . . . xiii Acknowledgments . . . . . . . . . . . xv Introduction . . . . . . . . . . . . xvii Part I — Getting Started 1. Installing Rails . . . . . . . . . . . . 3 Installing on Cloud9 4 Installing on a Virtual Machine 7 Installing on Windows 9 Installing on Mac OS X 12 Installing on Linux 14 Choosing a Rails Version 16 Setting Up Your Development Environment 16 Rails and Databases 20 2. Instant Gratification . . . . . . . . . . . 23 Creating a New Application 23 Hello, Rails! 26 Linking Pages Together 32 When Things Go Wrong 35 3. The Architecture of Rails Applications . . . . . . 39 Models, Views, and Controllers 39 Rails Model Support 42 Action Pack: The View and Controller 44 4. Introduction to Ruby . . . . . . . . . . 47 Ruby Is an Object-Oriented Language 47 • Contents vi Data Types 49 Logic 53 Organizing Structures 56 Marshaling Objects 59 Pulling It All Together 59 Ruby Idioms 60 Part II — Building an Application 5. The Depot Application . . . . . . . . . . 65 Incremental Development 65 What Depot Does 66 Let’s Code 70 6. Task A: Creating the Application . . . . . . . . 71 Iteration A1: Creating the Product Maintenance Application 71 Iteration A2: Making Prettier Listings 78 7. Task B: Validation and Unit Testing . . . . . . . 87 Iteration B1: Validating! 87 Iteration B2: Unit Testing of Models 91 8. Task C: Catalog Display . . . . . . . . . 103 Iteration C1: Creating the Catalog Listing 103 Iteration C2: Adding a Page Layout 107 Iteration C3: Using a Helper to Format the Price 112 Iteration C4: Functional Testing of Controllers 114 Iteration C5: Caching of Partial Results 116 9. Task D: Cart Creation . . . . . . . . . . 121 Iteration D1: Finding a Cart 121 Iteration D2: Connecting Products to Carts 122 Iteration D3: Adding a Button 125 10. Task E: A Smarter Cart . . . . . . . . . . 133 Iteration E1: Creating a Smarter Cart 133 Iteration E2: Handling Errors 138 Iteration E3: Finishing the Cart 143 11. Task F: Add a Dash of Ajax . . . . . . . . . 151 Iteration F1: Moving the Cart 152 Iteration F2: Creating an Ajax-Based Cart 159 • Contents vii Iteration F3: Highlighting Changes 164 Iteration F4: Hiding an Empty Cart with a Custom Helper 167 Iteration F5: Broadcasting Updates with Action Cable 169 12. Task G: Check Out! . . . . . . . . . . 175 Iteration G1: Capturing an Order 175 Iteration G2: Atom Feeds 189 13. Task H: Entering Additional Payment Details . . . . 195 Iteration H1: Adding Fields Dynamically to a Form 195 Iteration H2: Testing Our JavaScript Functionality 214 14. Task I: Processing Emails and Payments Efficiently . . . 217 Iteration I1: Sending Confirmation Emails 217 Iteration I2: Connecting to a Slow Payment Processor with Active Job 225 15. Task J: Logging In . . . . . . . . . . . 235 Iteration J1: Adding Users 235 Iteration J2: Authenticating Users 239 Iteration J3: Limiting Access 245 Iteration J4: Adding a Sidebar, More Administration 247 16. Task K: Internationalization . . . . . . . . 253 Iteration K1: Selecting the Locale 254 Iteration K2: Translating the Storefront 257 Iteration K3: Translating Checkout 265 Iteration K4: Adding a Locale Switcher 276 17. Task L: Deployment and Production . . . . . . 279 Iteration L1: Deploying with Phusion Passenger and MySQL 281 Iteration L2: Deploying Remotely with Capistrano 288 Iteration L3: Checking Up on a Deployed Application 294 Iteration L4: Deploying with Fewer Steps on Heroku 295 18. Depot Retrospective . . . . . . . . . . 301 Rails Concepts 301 Documenting What We’ve Done 304 • Contents viii Part III — Rails in Depth 19. Finding Your Way Around Rails . . . . . . . . 307 Where Things Go 307 Naming Conventions 315 20. Active Record . . . . . . . . . . . . 319 Defining Your Data 319 Locating and Traversing Records 324 Creating, Reading, Updating, and Deleting (CRUD) 328 Participating in the Monitoring Process 342 Transactions 348 21. Action Dispatch and Action Controller . . . . . . 353 Dispatching Requests to Controllers 354 Processing of Requests 363 Objects and Operations That Span Requests 375 22. Action View . . . . . . . . . . . . 385 Using Templates 385 Generating Forms 387 Processing Forms 390 Uploading Files to Rails Applications 391 Using Helpers 395 Reducing Maintenance with Layouts and Partials 402 23. Migrations . . . . . . . . . . . . . 411 Creating and Running Migrations 411 Anatomy of a Migration 414 Managing Tables 418 Advanced Migrations 423 When Migrations Go Bad 424 Schema Manipulation Outside Migrations 425 24. Customizing and Extending Rails . . . . . . . 427 Testing with RSpec 427 Creating HTML Templates with Slim 433 Serving CSS via Webpack 435 Customizing Rails in Other Ways 437 Where to Go from Here 438