Rails Recipes
Rails 3 Edition
by Chad Fowler
Version: P1.0 (March 2012)
Copyright 2012 The 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 Rails Recipes, Rails 3 Edition
Even the best chefs are loathe to re-create a recipe from scratch if they know a good one already exists. Rails programmers would do well to code like a great chef cooks and have this tome on their shelf.
David Heinemeier Hansson |
Creator of Ruby on Rails; partner at 37signals; coauthor of Agile Web Development with Rails; and blogger |
Rails Recipes is a great resource for any Rails programmer. The book is full of hidden gems (no pun intended) that many programmers may not discover in their daily quest to get the job done.
Gary Sherman |
Principal of GeoApt, LLC; chair of QGIS PSC; and author of The Geospatial Desktop |
Rails Recipes has always been the definitive guide for aspiring Rails developers. It doesnt just cover how you could build something, but delves into the details and explains all the reasons why you should build it that way. You can be sure that if you follow the tips and tricks in this book, youre on the right path.
Michael Koziarski |
Software developer, Rails Core team member, and partner, Southgate Labs |
Superlative. This readable, engaging book strikes a balance between laying out a practical solution to a problem and teaching the principles and thought processes behind it. You learn how to fix a problem today and gain the insight you need to avoid problems in the future.
Alex Graven |
Senior developer, Zeevex, a division of InComm |
Rails Recipes is a great book for any Rails developer. There is so much going on in the Rails community these days that I find it hard to keep all of it in context. This book provides the context I need.
Mike Gehard |
Lead software engineer, Living Social |
Introduction
What Makes a Good Recipe Book?
If I were to buy a real recipe bookyou know, a book about cooking foodI wouldnt be looking for a book that tells me how to dice vegetables or how to use a skillet. I can find that kind of information in an overview about cooking.
A recipe book is about how to make food you might not be able to easily figure out how to make on your own. Its about skipping the trial and error and jumping straight to a solution that works. Sometimes its even about making food you never imagined you could make.
If you want to learn how to make great Indian food, you buy a recipe book by a great Indian chef and follow his or her directions. Youre not buying just any old solution. Youre buying a solution you can trust to be good. Thats why famous chefs sell lots and lots of books. People want to make food that tastes good, and these chefs know how to make (and teach you how to make) food that tastes good.
Good recipe books do teach you techniques. Sometimes they even teach you about new tools. But they teach these skills within the context of and with the end goal of making something not just to teach them.
My goal for Rails Recipes is to teach you how to make great stuff with Rails and to do it right on your first try. These recipes and the techniques herein are extractions from my own work and from the great chefs of Rails: the Rails core developer team, the leading trainers and authors, and the earliest of early adopters.
I also hope to show you not only how to do things but to explain why they work the way they do. After reading through the recipes, you should walk away with a new level of Rails understanding to go with a huge list of successfully implemented hot new application features.
Whos It For?
Rails Recipes is for people who understand Rails and now want to see how an experienced Rails developer would attack specific problems. Like with a real recipe book, you should be able to flip through the table of contents, find something you need to get done , and get from start to finish in a matter of minutes.
Im going to assume you know the basics or that you can find them in a tutorial or an online reference. When youre busy trying to make something, you dont have spare time to read through introductory material. So if youre still in the beginning stages of learning Rails, be sure to have a copy of Agile Web Development with Rails [RTH11] and a bookmark to the Rails API documentation handy.
Rails Version
The examples in this book, except where noted, should work with Rails 3.1 or newer. All of the recipes that were part of the first edition of this book have been updated to Rails version 3.1, and several recipes cover new features that became available with that release.
Resources
The best place to go for Rails information is the Rails website. From there, you can find the mailing lists, IRC channels, and blogs of the Rails community.
Pragmatic Programmers has also set up a forum for Rails Recipes readers to discuss the recipes, help each other with problems, expand on the solutions, and even write new recipes. While Rails Recipes was in beta, the forum served as such a great resource for ideas that more than one reader-posted recipe made it into the book! The forum is at http://forums.pragprog.com/forums/8.
The books errata list is at http://books.pragprog.com/titles/rr2/errata. If you submit any problems you find, well list them there.
Youll find links to the source code for almost all of the books examples at http://www.pragmaticprogrammer.com/titles/rr2/code.html.
If youre reading the PDF version of this book, you can reportan error on a page by clicking the erratum linkat the bottom of the page, and you can get to the source codeof an example by clicking the gray lozenge containing thecodes filename that appears before the listing.
Acknowledgments
Thank you for reading this book. Thanks to everyone else who made the book what it is.