RAILS SOLUTIONS How to Use Ajax Techniques with Rails and When Not to Use Them How to Implement Other Web 2.0 Features in Rails, Such As Tag Clouds and Microformats
Total Page:16
File Type:pdf, Size:1020Kb
CYAN YELLOW MAGENTA BLACK Williams In this book you’ll learn: How to develop web applications quickly and easily with the Ruby on Rails framework The basics of programming principles and Ruby syntax, so you can begin to understand what’s going on under the hood How to implement must-have web application features, such as user accounts, data validation, sending e-mail, image uploading, and more RAILS SOLUTIONS How to use Ajax techniques with Rails and when not to use them How to implement other Web 2.0 features in Rails, such as tag clouds and microformats If you’re a web designer or developer who thinks that and usability are kept in mind throughout, ensuring that the coding involved in developing dynamic web your site both looks and works great. applications is too difficult, think again. This book, and The book begins by covering how to set up your the framework it covers, is the perfect solution to your computer as a Rails development environment needs. Ruby on Rails provides an easy-to-use method (including the MySQL database) and then follows with for quickly developing web applications, simplifying an introduction to Ruby and the basics of the Rails potentially complicated subjects such as web framework. Next, you are taken through several architecture, JavaScript, and SQL/database creation. practical examples that work together to build up a The simplicity of Rails belies its power, though—this complete modern web application, covering essential technology is used by major companies such as and useful website features such as user login, adding 37Signals and Google. and editing data, data validation, image uploading, and Rails Solutions: Ruby on Rails Made Easy is an much more. The book even covers more-advanced Rails introduction to Ruby on Rails with the web designer in topics such as the Rails test suite, plug-ins and mind. Instead of focusing on the intricate syntax of each components, debugging techniques, and deploying your method, the book focuses on the tasks you’ll want to web applications using Capistrano. Create dynamic web applications as perform on your website and then walks you through painlessly as possible—no programming how to implement that functionality with Rails. Design knowledge required Enhance your user experience easily Also Available with Ajax techniques—Rails makes it easy for you Filled with practical techniques you can use right away SHELVING CATEGORY 1. WEB DEVELOPMENT ISBN-13: 978-1-59059-752-1 ISBN-10: 1-59059-752-4 53499 Mac/PC compatible US $34.99 www.friendsofed.com Justin Williams 9 781590 597521 this print for reference only—size & color not accurate spine = 0.675" 288 page count 7524fm.qxd 12/13/06 4:41 PM Page i Rails Solutions Ruby on Rails Made Easy Justin Williams 7524fm.qxd 12/13/06 4:41 PM Page ii Rails Solutions: Ruby on Rails Made Easy Copyright © 2007 by Justin Williams 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-752-1 ISBN-10 (pbk): 1-59059-752-4 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. 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 www.springeronline.com. For information on translations, please contact Apress directly at 2560 Ninth Street, Suite 219, Berkeley, CA 94710. Phone 510-549-5930, fax 510-549-5939, e-mail [email protected], or visit www.apress.com. The information in this book is distributed on an “as is” basis, without warranty. Although every precaution 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 freely available to readers at www.friendsofed.com in the Downloads section. Credits Lead Editor Copy Editor Chris Mills Nancy Sixsmith Technical Reviewers Assistant Production Director Ashish Bansal Kari Brooks-Copony Ryan J. Bonnell Production Editor Editorial Board Katie Stence Steve Anglin Ewan Buckingham Compositor Gary Cornell Molly Sharp Jason Gilmore Jonathan Gennick Jonathan Hassell Artist James Huddleston April Milne Chris Mills Matthew Moodie Proofreader Dominic Shakeshaft Linda Seifert Jim Sumser Keir Thomas Indexer Matt Wade Michael Brinkman Project Manager Interior and Cover Designer Beth Christmas Kurt Krames Copy Edit Manager Manufacturing Director Nicole Flores Tom Debolski 7524fm.qxd 12/13/06 4:41 PM Page iii CONTENTS AT A GLANCE Introduction . xv Chapter 1: Introduction to Ruby on Rails . 3 Chapter 2: Installing Rails . 17 Chapter 3: Ruby for Rails Developers . 39 Chapter 4: Getting Started with Rails . 53 Chapter 5: More Advanced Rails . 83 Chapter 6: Formatting Data . 107 Chapter 7: Introduction to Ajax . 117 Chapter 8: Bringing Forms to Life with Ajax . 127 Chapter 9: Uploading Files and Sending Attachments . 149 Chapter 10: User Authentication and Session Management . 161 Chapter 11: Customizing Rails Views . 183 7524fm.qxd 12/13/06 4:41 PM Page iv CONTENTS AT A GLANCE Chapter 12: Using Rails Plug-ins and Engines . 203 Chapter 13: Deploying with Capistrano . 223 Appendix A: Caching Your Content . 239 Appendix B: Testing Rails . 247 Index. 261 iv 7524fm.qxd 12/13/06 4:41 PM Page v CONTENTS Introduction . xv Chapter 1: Introduction to Ruby on Rails . 3 A history of Ruby . 5 Object-oriented programming . 6 Riding the Rails . 7 Components of Rails . 8 Model, View, Controller . 11 Rails’ database support . 12 Who uses Rails? . 12 Is Rails safe? . 14 Summary . 14 Chapter 2: Installing Rails . 17 Installing Rails on Mac OS X . 18 Installing Xcode . 19 Semiautomatic Rails install . 21 Manual install . 22 Setting the file path correctly . 22 Setting up Ruby on Mac OS X . 23 Installing the Rails framework on Mac OSX...finally! . 24 Installing FastCGI . 24 Installing Lighttpd . 25 Installing MySQL on Mac OSX . 26 Locomotive . 27 Extra tools for Mac OSX . 28 7524fm.qxd 12/13/06 4:41 PM Page vi CONTENTS Installing Rails on Windows . 28 Installing Ruby on Windows . 28 Installing MySQL on Windows . 30 InstantRails—Rails for Windows . 35 Extra tools for Windows . 35 Ready? Let’s do it! . 36 Chapter 3: Ruby for Rails Developers . 39 Basic Ruby syntax . 40 Ruby variables . 41 Classes and objects . 42 Inheritance . 46 Polymorphism . 47 Calling objects . 48 Arrays and hashes . 48 Decision structures . 49 while . 50 Iterators . 50 Exception handling . 51 Summary . 51 Chapter 4: Getting Started with Rails . 53 Creating a Rails project . 56 Configuring the web server . 57 Viewing the application . 58 Creating the database . 59 Windows . 60 Mac OS X . 60 Using the command line . 61 Telling Rails about the databases . 62 Creating the model . 63 Rails migrations . 64 Creating the controller . 67 Creating the views . ..