Rails for PHP Developers
Total Page:16
File Type:pdf, Size:1020Kb
What readers are saying about Rails for PHP Developers This is a thorough and approachable introduction to Ruby and Rails for PHP programmers from fellow developers who are well-versed in both Ruby and PHP. Paul M. Jones Lead Developer on the Solar Framework for PHP As a PHP developer, I found the book focused well on the transition from coding PHP to coding Ruby (and Rails) and that it gave great examples of translating common PHP idioms to Ruby. Matthew Weier O’Phinney PHP Developer and Zend Framework Core Contributor The quality of the writing is superb, the challenges and examples are engaging, and the PHP to Ruby information is a valuable resource. The exercises are nice, are short, and follow the topic well, giving readers some creative time between each chapter. Mislav Marohnic´ Prototype JavaScript Framework Core Developer This is an enjoyable book packed with great information and usable examples. I like the organization of the book and the gentle, infor- mal voice with which the authors cover many complex topics. It’s easy to read, yet it has plenty of substance and depth to give the reader a great introduction to Rails. Bill Karwin MySQL Guild and Former Zend Framework Project Leader Rails for PHP Developers Derek DeVries Mike Naberezny The Pragmatic Bookshelf Raleigh, North Carolina Dallas, Texas Many of the designations used by manufacturers and sellers to distinguish their prod- ucts 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://www.pragprog.com Copyright © 2008 Derek DeVries and Mike Naberezny. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmit- ted, in any form, or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior consent of the publisher. Printed in the United States of America. ISBN-10: 1-934356-04-2 ISBN-13: 978-1-9343560-4-3 Printed on acid-free paper with 50% recycled, 15% post-consumer content. First printing, January 2008 Contents Acknowledgments 10 Preface 11 What Rails Offers .......................... 11 Who Should Read This Book ................... 12 Resources .............................. 12 PHP and Rails: A Personal View .................. 13 About the Code Examples ..................... 14 About the Environment Used ................... 14 Version Requirements ....................... 15 How to Read This Book ....................... 15 I From PHP to Rails 17 1 Getting Started with Rails 18 1.1 Rails as an Extension of Ruby .............. 18 1.2 The Components of Rails ................. 19 1.3 Opinionated Software ................... 20 1.4 The MVC Pattern and Rails ................ 22 1.5 Installing Ruby and Rails ................. 24 1.6 Creating a Rails App .................... 25 1.7 Chapter Review ....................... 42 1.8 Exercises ........................... 42 2 Beginning Ruby Code 43 2.1 Seeing Ruby as a General-Purpose Language . 43 2.2 Interacting with Ruby ................... 45 2.3 Objectifying Everything .................. 47 2.4 Accepting Ruby’s Object World .............. 48 2.5 Assigning to Variables ................... 50 2.6 Writing Methods and Passing Parameters . 53 2.7 Controlling Program Flow ................. 57 CONTENTS 6 2.8 Handling Errors ....................... 59 2.9 Understanding Blocks ................... 65 2.10 Chapter Review ....................... 70 2.11 Exercises ........................... 71 3 Embracing the Ruby Philosophy 72 3.1 Thinking in Objects .................... 72 3.2 Understanding Attributes ................. 75 3.3 Method Visibility ...................... 77 3.4 Understanding Typing ................... 79 3.5 Implementing Interfaces with Mixins .......... 84 3.6 Organizing Code with Namespaces ............ 88 3.7 Overriding Operators .................... 91 3.8 Reopening Classes ..................... 93 3.9 Chapter Review ....................... 95 3.10 Exercises ........................... 95 II Building a Rails Application 96 4 Modeling the Domain 97 4.1 Defining Requirements .................. 98 4.2 Using the Database .................... 101 4.3 Creating the Application .................. 104 4.4 Generating the First Model ................ 106 4.5 Building Database Tables ................. 108 4.6 Employing ActiveRecord .................. 113 4.7 Chapter Review ....................... 118 4.8 Exercises ........................... 119 5 Working with Controllers and Views 120 5.1 Identifying Resources ................... 120 5.2 Creating Controllers .................... 122 5.3 Routing Requests ...................... 125 5.4 Retrieving Meeting Data .................. 129 5.5 Viewing Meetings ...................... 131 5.6 Adding Links ........................ 132 5.7 Creating New Meetings .................. 138 5.8 Redirection and Flash Data ................ 143 5.9 Administrating Meetings .................. 145 5.10 Separating Public Files .................. 153 5.11 Adding a Layout ...................... 155 CONTENTS 7 5.12 Chapter Review ....................... 158 5.13 Exercises ........................... 160 6 Validating and Testing Models 161 6.1 Validating Model Data ................... 161 6.2 Using Rails Environments ................. 164 6.3 Testing Our Models ..................... 165 6.4 Chapter Review ....................... 171 6.5 Exercises ........................... 172 7 Authenticating Users 173 7.1 Migrating to a More Secure User ............. 173 7.2 User Registration ...................... 175 7.3 Viewing and Editing Users ................ 186 7.4 Restoring Sessions ..................... 192 7.5 LoggingIn .......................... 195 7.6 Chapter Review ....................... 200 7.7 Exercises ........................... 200 8 Defining Associations 202 8.1 Connecting Presentations ................. 202 8.2 Testing Associations .................... 205 8.3 Integrating Presentations into Meetings ......... 207 8.4 Routing Presentations ................... 208 8.5 The Presentation Controller ................ 210 8.6 Spring Cleaning ....................... 215 8.7 Chapter Review ....................... 221 8.8 Exercises ........................... 222 9 Preparing to Launch 223 9.1 Adding the Home Page ................... 223 9.2 Securing Our Actions ................... 226 9.3 Protecting from Mass Assignment ............ 232 9.4 Caching the Pages ..................... 234 9.5 Chapter Review ....................... 240 9.6 Exercises ........................... 240 10 Deploying the Application 242 10.1 Choosing a Host ...................... 243 10.2 The Production Environment ............... 245 10.3 Preparing Our Application ................. 246 10.4 Preparing Our Deployment Server ............ 248 CONTENTS 8 10.5 Launching the Application ................ 253 10.6 Enhancing Performance .................. 256 10.7 Scaling Your Application .................. 260 10.8 Chapter Review ....................... 261 10.9 Exercises ........................... 262 III PHP to Ruby at a Glance 263 11 PHP to Ruby Basics Reference 264 11.1 Basic Syntax ........................ 264 11.2 Basic Data Types ...................... 266 11.3 Variables ........................... 284 11.4 Constants .......................... 292 11.5 Expressions ......................... 294 11.6 Operators .......................... 296 11.7 Control Structures ..................... 305 12 PHP to Ruby Advanced Reference 316 12.1 Blocks ............................ 316 12.2 Functions .......................... 318 12.3 Classes and Objects .................... 326 12.4 Exceptions .......................... 354 12.5 References .......................... 355 12.6 External Libraries and Packages ............. 357 12.7 Documenting Code ..................... 359 13 PHP to Rails Reference 371 13.1 Templates .......................... 371 13.2 $_GET/$_POST ....................... 372 13.3 $_FILES ........................... 373 13.4 $_SERVER .......................... 376 13.5 Cookies ............................ 376 13.6 Sessions ........................... 378 13.7 Headers and Redirection ................. 380 13.8 Security ........................... 381 13.9 Debugging .......................... 386 13.10 Accessing the Database .................. 388 13.11 Email ............................. 390 13.12 Testing Rails Code ..................... 391 13.13 Rails Plug-Ins ........................ 396 CONTENTS 9 Bibliography 399 Index 400 Acknowledgments Derek would like to thank Melissa, daVinci, and his new baby girl, Sevilla, who was born during the writing of this preface. Mike would like to thank Kathy for her support and his parents for buying his first computer, the Commodore 64. We’d like to thank our reviewers: Bill Karwin, Mislav Marohnic, Tim Fletcher, Paul M. Jones, Matthew