Beginning Rails from Novice to Professional.Pdf
Total Page:16
File Type:pdf, Size:1020Kb
CYAN YELLOW MAGENTA BLACK PANTONE 123 C BOOKS FOR PROFESSIONALS BY PROFESSIONALS® THE EXPERT’S VOICE® IN WEB DEVELOPMENT Companion eBook Available Beginning Rails: Beginning From Novice to Professional Dear Reader, This book is for anyone who wants to learn how to develop web applications using the Rails framework for Ruby. Even if you have no prior programming Beginning experience or have never built a web application before, you’ll be able to get up and running with Rails using this book. Starting with a thorough introduction Rails to the Rails landscape, we’ll walk you though creating a working application, from installation to deployment and everything in between. Rails is modular for a reason: software is easier to write and maintain when it’s divided according to areas of concern. We think this approach applies to teaching as well. That’s why each chapter in this book focuses on a specific component of the framework. From modeling relationships with Active Record to sending mail using Action Mailer, we show you how the components work together and where the lines of responsibility are drawn. We do this in a way that you can understand, by walking through code examples and explaining why things are the way they are. The result is that you’ll know precisely how the pieces fit together. Rails is optimized for what most people need to do, most of the time, and Rails this book is no different. Our goal is to teach you the most important concepts and no more. This is not an exhaustive reference, nor is it filled with ivory tower exercises. Instead, we’ve focused on the techniques that we, as Rails developers, use on a daily basis. From Novice to Professional In this book, you’ll find advice and practical solutions to common problems. We’ll show you what we think are the best tools and techniques to use when working with Rails, and what we’ve found to be best practices. We hope you’ll find it useful, informative, and even entertaining. Jeffrey Allan Hardy and Cloves Carneiro Jr. A Beginner’s Guide to the Rails Framework for Ruby Companion eBook S ITLE T See last page for details on $10 eBook version RELATED RELATED Carneiro SOURCE CODE ONLINE ISBN-13: 978-1-59059-686-9 Hardy, and www.apress.com ISBN-10: 1-59059-686-2 Jeffrey Allan Hardy Cloves Carneiro Jr. 5 3 4 9 9 with Hampton Catlin US $34.99 Shelve in Programming/ Web Development User level: 9 781590 596869 Beginner–Intermediate this print for content only—size & color not accurate spine = x.xxx" xxx page count 686-2 FM.qxd 6/30/07 9:04 AM Page i Beginning Rails From Novice to Professional Jeffrey Allan Hardy and Cloves Carneiro Jr. with Hampton Catlin 686-2 FM.qxd 6/30/07 9:04 AM Page ii Beginning Rails: From Novice to Professional Copyright © 2007 by Jeffrey Allan Hardy and Cloves Carneiro Jr. All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN-13 (pbk): 978-1-59059-686-9 ISBN-10 (pbk): 1-59059-686-2 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. Lead Editor: Chris Mills Technical Reviewer: Hampton Catlin Editorial Board: Steve Anglin, Ewan Buckingham, Gary Cornell, Jonathan Gennick, Jason Gilmore, Jonathan Hassell, Chris Mills, Matthew Moodie, Jeffrey Pepper, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Project Manager: Sofia Marchant Copy Edit Manager: Nicole Flores Copy Editor: Marilyn Smith Assistant Production Director: Kari Brooks-Copony Production Editor: Ellie Fountain Compositor: Gina Rexrode Proofreader: Lori Bring Indexer: Broccoli Information Management Artist: Kinetic Publishing Services, LLC Cover Designer: Kurt Krames Manufacturing Director: Tom Debolski Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail [email protected], or visit http://www.springeronline.com. For information on translations, please contact Apress directly at 2855 Telegraph Avenue, Suite 600, Berkeley, CA 94705. Phone 510-549-5930, fax 510-549-5939, e-mail [email protected], or visit http://www.apress.com. The information in this book is distributed on an “as is” basis, without warranty. Although every precau- tion has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. The source code for this book is available to readers at http://www.apress.com in the Source Code/ Download section. You will need to answer questions pertaining to this book in order to successfully download the code. 686-2 FM.qxd 6/30/07 9:04 AM Page iii For my best friend, Amy. —Jeff For Jane; you inspire me every morning. —Cloves 686-2 FM.qxd 6/30/07 9:04 AM Page iv 686-2 FM.qxd 6/30/07 9:04 AM Page v Contents at a Glance About the Authors . xv About the Technical Reviewer. xvii Acknowledgments . xix Preface. xxi Introduction . xxiii ■CHAPTER 1 Introducing the Rails Framework. 1 ■CHAPTER 2 Getting Started . 17 ■CHAPTER 3 Getting Something Running . 45 ■CHAPTER 4 Working with a Database: Active Record. 65 ■CHAPTER 5 Advanced Active Record: Enhancing Your Models . 89 ■CHAPTER 6 Action Pack: Working with the View and the Controller . 133 ■CHAPTER 7 Improving Interaction with Ajax . 201 ■CHAPTER 8 Sending and Receiving Mail. 229 ■CHAPTER 9 Testing Your Application. 245 ■CHAPTER 10 Extending Rails with Plugins . 279 ■CHAPTER 11 Deploying Your Rails Applications . 301 ■APPENDIX A Ruby, a Programmer’s Best Friend . 315 ■APPENDIX B Databases 101 . 331 ■APPENDIX C The Rails Community . 341 ■INDEX . 345 v 686-2 FM.qxd 6/30/07 9:04 AM Page vi 686-2 FM.qxd 6/30/07 9:04 AM Page vii Contents About the Authors . xv About the Technical Reviewer. xvii Acknowledgments . xix Preface. xxi Introduction . xxiii ■CHAPTER 1 Introducing the Rails Framework . 1 The Rise and Rise of the Web Application. 1 The Web Is Not Perfect . 3 The Good Web Framework. 3 Enter Rails. 4 Rails Is Ruby . 5 Rails Encourages Agility . 6 Rails Is Opinionated Software . 9 Rails Is Open Source . 9 The MVC Pattern . 10 The MVC Cycle . 11 The Layers of MVC. 12 The Libraries That Make Up Rails. 15 Rails Is No Silver Bullet. 15 Summary . 16 ■CHAPTER 2 Getting Started . 17 An Overview of Rails Installation. 17 Installing on Mac OS X 10.4 Tiger . 19 Installing the Apple Developer Tools (Xcode). 20 Installing MySQL . 20 Adding MySQL to Your PATH . 23 Installing Ruby . 24 vii 686-2 FM.qxd 6/30/07 9:04 AM Page viii viii ■CONTENTS Installing RubyGems . 26 Installing Rails . 27 Automating Installation . 27 Installing on Windows XP . 28 Installing MySQL . 29 Installing Ruby . 30 Installing Rails . 31 Installing on Linux . 32 Installing MySQL . 33 Installing Ruby . ..