ebook img

Beginning Rails: From Novice to Professional PDF

386 Pages·2007·2.74 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Beginning Rails: From Novice to Professional

cyan yelloW maGenTa Black panTone 123 c Books for professionals By professionals® The eXperT’s Voice® in WeB DeVelopmenT Companion eBook Available Beginning Rails: B From Novice to Professional e g Beginning Dear Reader, i n This book is for anyone who wants to learn how to develop web applications n using the Rails framework for Ruby. Even if you have no prior programming i n experience or have never built a web application before, you’ll be able to get up g 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. R 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 a 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 i together and where the lines of responsibility are drawn. We do this in a way l that you can understand, by walking through code examples and explaining s 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 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 From Novice to Professional exercises. Instead, we’ve focused on the techniques that we, as Rails developers, use on a daily basis. 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 e l t ti See last page for details d e on $10 eBook version at l e R C Jeffrey Allan Hardy Cloves Carneiro Jr. SOURCE CODE ONLINE ISBN-13: 978-1-59059-686-9 a and rH www.apress.com ISBN-10: 1-59059-686-2 n a 53499 er with Hampton Catlin ird oy 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.comin 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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 Installing RubyGems. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 Installing Rails . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 Creating Your First Rails Application. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 Starting the Built-In Web Server. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 Generating a Controller. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 Creating an Action. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 Creating a Template . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 ■ CHAPTER 3 Getting Something Running. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 An Overview of the Project. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 Creating the Events Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 Creating the Project Databases. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 Creating the Event Model. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 Creating a Database Table. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 Generating a Controller. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 Up and Running with Scaffolding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 Adding More Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 Adding Validations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 Generating a Scaffold. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 686-2 FM.qxd 6/30/07 9:04 AM Page ix ■ CONTENTS ix ■ CHAPTER 4 Working with a Database: Active Record. . . . . . . . . . . . . . . . . . 65 Introducing Active Record:Object-Relational Mapping on Rails. . . . . . . . 66 What About SQL?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 Active Record Conventions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 Introducing the Console. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 Active Record Basics:CRUD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 Creating New Records . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 Reading (Finding) Records. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 Updating Records. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 Deleting Records. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 When Good Models Go Bad. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 ■ CHAPTER 5 Advanced Active Record: Enhancing Your Models. . . . . . . . 89 Adding Methods. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 Using Associations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 Declaring Associations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 Creating One-to-One Associations. . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 Creating One-to-Many Associations. . . . . . . . . . . . . . . . . . . . . . . . . . . 99 Applying Association Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 Creating Many-to-Many Associations . . . . . . . . . . . . . . . . . . . . . . . . 107 Creating Rich Many-to-Many Associations. . . . . . . . . . . . . . . . . . . . 112 Building Conditions for Advanced Finding. . . . . . . . . . . . . . . . . . . . . . . . . . 115 Using a Conditions Hash. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 Using a SQL Fragment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116 Using an Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 Using Association Proxies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 Applying Validations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120 Building Custom Validation Methods . . . . . . . . . . . . . . . . . . . . . . . . . 121 Using Built-in Validations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 Making Callbacks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 Reviewing the Updated Models. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 Updating the User Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132

Description:
Ruby on Rails has taken the world by storm. First released in the mid-2004, this open source framework has single-handedly changed the landscape of Web development. It’s both powering and defining the "Web 2.0" revolution on the web, providing a radical shift in the way we think about web applicat
See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.