The Rails View
Creating a Beautiful and Maintainable User Experience
by John Athayde, Bruce Williams
Version: P1.1 (April 2012)
Copyright 2012 Pragmatic Programmers, LLC. This book is licensed tothe individual who purchased it. We don't copy-protect itbecause that would limit your ability to use it for yourown purposes. Please don't break this trustyou can use this across all of your devices but please do not share this copywith other members of your team, with friends, or via file sharing services. Thanks.
Dave & Andy.
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://pragprog.com.
Table of Contents
Copyright 2012, The Pragmatic Bookshelf.
What readers are saying about The Rails View
This is a must-read for Rails developers looking to juice up their skills for a world of web apps that increasingly includes mobile browsers and a lot more JavaScript.
Yehuda Katz |
Driving force behind Rails 3.0 and Co-founder, Tilde |
In the past several years, Ive been privileged to work with some of the worlds leading Rails developers. If asked to name the best view-layer Rails developer Ive met, Id have a hard time picking between two names: Bruce Williams and John Athayde. This book is a rare opportunity to look into the minds of two of the leading experts on an area that receives far too little attention. Read, apply, and reread.
Chad Fowler |
VP Engineering, LivingSocial |
Finally! An authoritative and up-to-date guide to everything view-related in Rails 3. If youre stabbing in the dark when putting together your Rails apps views, The Rails View provides a big confidence boost and shows how to get things done the right way.
Peter Cooper |
Editor, Ruby Inside and Ruby Weekly |
The Rails view layer has always been a morass, but this book reins it in with details of how to build views as software, not just as markup. This book represents the wisdom gained from years worth of building maintainable interfaces by two of the best and brightest minds in our business. I have been writing Ruby code for over a decade and Rails code since its inception, and out of all the Ruby books Ive read, I value this one the most.
Rich Kilmer |
Director, RubyCentral |
Acknowledgments
We have many people to thank for making this very ambitious book possible.
First of all, as this is a book about Rails, a lot of credit must go to the creator of the framework, David Heinemeier Hansson, the members of rails-core (past and present), and other contributors. The ideas in this book are distilled from years of discussion and collaboration with the Rails and Ruby communities.
Throughout our careers weve drawn inspiration and motivation from a number of web luminaries, and we would be remiss in failing to mention at least a few of them: Dan Cederholm, Molly Holzschlag, Paul Irish, Jeremy Keith, Steve Krug, Eric Meyer, Jakob Nielsen, Mark Pilgrim, and Jeffrey Zeldman.
We were surprised to learn that a number of people actually volunteered to read the book before it was complete, thereby putting their own sanity at risk. Wed like to thank these brave souls for their help in identifying issues, suggesting topics, and otherwise vastly improving the text: Derek Bailey, Kevin Beam, David A. Black, David Bock, Daniel Bretoi, Jeff Casimir, BJ Clark, Jeff Cohen, Justin Dell, Joel Friedman, Jeremy Hinegardner, Mark Margolis, Dan Reedy, Sam Rose, Loren Sands-Ramshaw, Diego Scataglini, Tibor Simac, Charley Stran, Mark Tabler, and Lynn M. Wallenstein.
This book simply would not have been completed if not for our amazing editor, Brian Hogan. He continuously challenged our preconceptions and helped to clarify our intent, all with seemingly unbounded patience and class. And we promise, Brian, well never again utilize utilize in our writing (except for that time right there).
Many thanks to Rich Kilmer, Chad Fowler, Aaron Batalion, and our colleagues in the engineering, design, and product teams at LivingSocial. You keep us hungry to win every day, constantly building pressure to innovate, which makes us better designers and developers.
John would like to thank his supportive wife, Whitney, for her patience and encouragement throughout the process; his parents, grandparents, and extended family for their love and support and for purchasing that Mac SE back in the day with Hypercard installed; all the members of #caboose for their patience and discussion over the years; Justin Hankins and Sara Flemming for all the years of experimenting in HTML, CSS, and Rails with Meticulous; and Amy Hoy for an intense year of business, design, and development boot camp while running Hyphenated People with him. He also thanks Bruce for agreeing to be a coauthor so that this book could rise to its potential.
Bruce credits the care and support of his wife, Melissa, and his two sons, Braedyn and Jamis, for the uncharacteristic level of determination and attention hes managed to focus on this single project, which broke any number of personal records. Also, Bruces life would have turned out very differently were it not for the love of his mother, Monique, and father, Bruce (the elder), and a varied and distributed family hes proud to call his own, even if they do occasionally call him for tech support. To his coauthor, Bruce offers an equal share of sincere thanks and rampant design skill jealousy. Some things do not change. Finally, Bruce would like to dedicate his work on this book to the memory of his brother, Tristan Eppler.
John Athayde & Bruce Williams
March 2012
john@therailsview.com
| bruce@therailsview.com
Copyright 2012, The Pragmatic Bookshelf.
Preface
In 2004, Rails was born and the web discovered the MVC (model-view-controller) pattern in earnest, which brought a whole new level of productivity and fun to a world of developers and designers.
Youll find no end of books that provide a firm foundation for writing controllers and models (which benefit greatly from being written top-to-bottom in plain Ruby), but when it comes to viewsthat meeting place of Ruby, HTML, JavaScript, and CSS (not to mention developers and designers)whats a disciplined craftsman to do?
This book aims to widen the discussion of Rails best practices to include solid, objective principles we can follow when building and refactoring views. By the time youre finished reading, youll understand how you can structure your front end to be less brittle and more effective and boost your teams productivity.