www.dbebooks.com - Free Books & magazines Aptana RadRails: An IDE for Rails Development Develop Ruby on Rails applications fast using RadRails 1.0 Community Edition A comprehensive guide to using RadRails to develop your Ruby on Rails projects in a professional and productive manner Javier Ramírez BIRMINGHAM - MUMBAI Aptana RadRails: An IDE for Rails Development Develop Ruby on Rails applications fast using RadRails 1.0 Community Edition Copyright © 2008 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, Packt Publishing, nor its dealers or distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. First published: May 2008 Production Reference: 1190508 Published by Packt Publishing Ltd. 32 Lincoln Road Olton Birmingham, B27 6PA, UK. ISBN 978-1-847193-98-8 www.packtpub.com Cover Image by Nilesh R. Mohite ([email protected]) Credits Author Project Manager Javier Ramírez Abhijeet Deobhakta Reviewer Indexer Chris Williams Hemangini Bari Acquisition Editor Proofreader Shayantani Chaudhuri Chris Smith Adil Rizwan Production Coordinator Technical Editor Shantanu Zagade Bhupali Khule Cover Work Editorial Team Leader Shantanu Zagade Akshara Aware About the Author Javier Ramírez has been developing Web Applications since before the term Web Application was coined. Born in Zaragoza, Spain, in 1974, he started programming as a hobby around the age of 11 assisted by his older sister. A few years later, he got his first modem and became a regular of BBSes and Newsgroups. His interest in developing server applications that can be accessed remotely comes from those times. He has learned—and forgotten—many programming languages, including Basic, dBase III, Cobol, Pascal, SQL, C, C++, ASP, TCL, JavaScript, PHP, and Java, the language on which he has focused for most of his career so far. He has held the positions of programmer, analyst, consultant, team leader, post-sales engineer, project manager, and software architect, totaling over 12 years in the IT business. Having developed projects mainly for banks and other big corporations in Spain, Italy, and the US, he co-founded some years ago a small software development shop, which provided him with valuable experience about the difficulties and the joys of entrepreneurship. After two years, he left the company in pursuit of new professional challenges. For the last two years, he has been proudly working for ASPgems, where he discovered Ruby on Rails, which soon became his framework of choice for developing Web Applications. He is one of the organizers of the Spanish Rails Conference, also participating as a Speaker in the two events held so far. He has also been an instructor on Robotics, Java, FatWire Content Server, and Ruby on Rails, and a University Lecturer in the subjects of 'Software Engineering' and 'The Java Programming Language', which he currently teaches at Universidad Francisco de Vitoria, in Madrid. Javier Ramírez holds a B.Sc. in Business Information Systems with First Class Honors and a degree in Ingeniería en Sistemas de Computación This book would have not been possible in its present form if not for the work of the people at Packt Publishing. I'd like to thank specially to Acquisition Editor Shayantani Chaudhuri for giving me the opportunity to write this book and to Technical Editor Bhupali Khule for polishing the rough edges, which were many. Abhijeet, Adil, Shantanu, and Patricia also deserve to be in this list. I would also like to acknowledge the fantastic work of the original team of RadRails, and also of the developers behind Eclipse. A big special thank you goes to Chris Williams, the current lead developer of Aptana RadRails and the technical reviewer for this book. He provided me with precious insight about a large number of issues. Of course, any errors that might remain in the book are my own. Thanks to the people at ASPgems, for trusting me to join one of the most gifted team I know and for introducing me to Ruby on Rails. I also have to thank Madzia, who gently allowed me to disappear for uncountable evenings and weekends during the last nine months without complaining—or without complaining much anyhow. Finally, but not least important, I would like to thank my parents for supporting my education. A part of this book—the good one—wouldn't have been possible without all of them. About the Reviewer Chris Williams has spent the last four years working on the Ruby Development Tools project to bring Ruby tooling to the Eclipse IDE, and has been the lead developer of RadRails since joining Aptana in the first half of 2007. Prior to joining Aptana, Chris has worked in R&D for both Paychex Inc. and the Xerox Corporation. Chris lives with his wife and dog in Rochester, New York. Thanks to Kyle Shank, Matt Kent, and Marc Baumbach for creating RadRails; Markus Barchfeld for his work on RDT's debugger and builds; Jason Morrison for his work on type inferrencing; Mirko Stocker, Thomas Corbat, and Lukas Felber for their addition of refactoring support; Adam Williams for starting the RDT project; Paul Colton and Aptana for allowing me to work on RadRails full-time. Lastly, I'd like to thank my wonderful wife, Lidza, for being patient and sharing a passion for our careers; and my dog Beaker for knowing when I need to take a walk. Table of Contents Preface 1 Chapter 1: Getting Started 7 Do I Need an IDE for Rails Development? 7 About Aptana RadRails 9 How Can Aptana RadRails Help Me? 10 Prerequisites 11 Java Virtual Machine 11 Ruby and Rails 12 Components to Install 13 Installing Rails if you already have Ruby and RubyGems 13 Installing Ruby and Rails on Linux 15 Installing Ruby and Rails on OS X 16 Installing Ruby and Rails on Windows 17 Supported Databases 17 Installing Eclipse 18 Installing Plugins in Eclipse 20 RadRails Installation 21 Summary 25 Chapter 2: First Steps 27 Basic Configuration 27 Eclipse Preferences Dialog 27 File Encoding 30 Connecting through a Proxy 30 Ruby Environment 32 Rails Environment 33 Creating a Rails Project 34 Importing an Existing Project into RadRails 37 Working with Perspectives and Views 38 Eclipse Perspectives 38 Table of Contents Eclipse Views 39 Summary 43 Chapter 3: Your First Application 45 Basic Views 45 The Ruby Explorer View 46 Ruby Explorer Top Icons 50 The Console View 51 The Generators View 53 Generating Models and Migrations 55 Running Your Migrations 56 Generating Scaffolds 58 Starting Your Server 60 Monitoring Your Server 63 Summary 65 Chapter 4: Writing Ruby Code 67 A Quick Note about Keyboard Shortcuts 67 The Ruby Editor 68 Syntax Highlighting 69 Outlining the Structure of Your Ruby Code 70 Quick Outline 71 Type Hierarchy 75 General Outline View 77 Code Folding 78 Code Formatting 80 Indenting Code Blocks 82 Commenting Code Blocks 82 Code Completion 82 Code Templates 85 Defining Your Own Code Templates 87 Navigating Your Code 89 General Source Navigation Tools 89 Matching Brackets 89 Declarations of Classes, Modules, Methods, and Variables 90 Navigating Your MVC Code 91 Opening Types and Resources 92 Refactoring 94 Generate Accessors 95 Generate Constructors 96 Convert Local Variable to Field 96 Encapsulate Field 96 [ ii ] Table of Contents Extract Method 96 Extract Constant 98 Inline Method 98 Rename 98 Split Local Variable 99 Searching in Ruby Projects 100 Searching within the Current File 100 Searching across Multiple Files 101 Ruby Search 104 Call Hierarchy 105 Summary 106 Chapter 5: Coding Rails Views 107 ERB/RHTML Templates 107 Views Navigation 108 View Templates 110 HTML Code Assist 112 Refactoring into Partials 113 Outline 114 Editing HTML Files 115 Editing JavaScript Files 115 Editing CSS Files 118 Summary 121 Chapter 6: Debugging Your Application 123 Getting Started with Debugging 124 Debugger Configuration 124 Starting Your Server 126 Debugging a Ruby Script 126 Using Breakpoints 127 The Breakpoints View 130 Setting Generic Breakpoints for Exceptions 131 Exporting and Importing Breakpoints 133 The Debug View 134 The Debug View and the Stack Frame 134 Stepping through Your Application 136 Variables and Expressions 139 The Variables View 139 The Expressions View 142 The Display View 144 Useful Tools for Debugging 146 Linking Errors and Source Code from the Browser 146 [ iii ]