Rails for PHP Developers, The Pragmatic Programmers

Derek DeVries

Mike Naberezny

R a i l s F o r P H P D e v e l o p e r

The Pragmatic Bookshelf
Raleigh, North Carolina Dallas, Texas




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 Weier O’Phinney, Dallas DeVries,
Laura Thomson, and Chuck Hagenbuch. Their expertise, time, and
effort have been invaluable to us.

We’d like to thank the Pragmatic Programmers for giving us a great
opportunity to spread the word of Rails and our editor, Susannah, for
keeping us on track.

Thanks to everyone building open source software that we use and love,
from Rails to PHP. They truly make our working lives so much easier to enjoy.


Preface

There is no doubt that by now you’ve heard all of the hype about Ruby
on Rails. It has been generating a lot of buzz with the promise of making
web applications fast and simple to create, and you may be wondering
what the big deal is. We know that PHP has been doing this for years
and has proven quite capable by its use in large companies such as
Yahoo. You may be wondering whether it’s worth the time investment
to learn Rails and Ruby, when you already have PHP under your belt.

What Rails Offers
Rails embraces a general development philosophy that sets a high priority
on creating maintainable code. By following some simple guidelines,
you should be able to keep a uniform pace of development and be
free to change your code with little fear of breaking existing functionality.
Rails achieves this by cherry-picking proven web development patterns
and best practices. These are two of the most important principles
Rails follows:
• Convention over configuration
• Don’t repeat yourself (DRY)
Rails defines the directory structure of your application for you and sets
a series of conventions for naming files, classes, and database tables.
It takes advantage of these conventions to tie together your application
without a lot of configuration. You may initially be resistant to the
idea of Rails telling you how to structure your application, but your
first Rails application will quickly demonstrate the efficiency that these
conventions offer you. By choosing smart defaults, Rails allows you to
focus on the functionality of your application instead of on the skeleton.
Rails developers tend to be almost religious about the DRY principle.
Functionality is written cleanly once, and only once. Rails provides an
environment that makes it easy to consolidate shared code between
different components of your application.

Rails gives first-class importance to testing. Writing code is always done
in parallel with tests to ensure the code works as intended and will
continue to work when things around it change. In PHP, the uptake of
testing culture has been slow, and the methodologies for testing entire
applications are not clear. Ruby’s dynamic and flexible object model,
along with its standard library, makes unit testing easy. The Rails stack
builds on this to provide clear, built-in support for testing all parts of a
web application from the first line of code.

Who Should Read This Book
This book is meant for PHP developers who are interested in adding
Rails to their toolsets. There are a lot of books on Rails now, but PHP
developers have a unique way of thinking about problems that are built
around the PHP mind-set. This book aims to guide your learning in
Rails based on your existing knowledge of programming in PHP An
understanding of object-oriented programming in PHP will help but is
not entirely necessary. This should be something you start to pick up
naturally while programming in Ruby.

Through this book, you will likely learn valuable lessons from Rails
that will inform future PHP development. Rails assembles a collection
of patterns and practices that are not new in themselves. Many of the
patterns in Rails can be implemented in other languages and may help
inspire some new approaches in your PHP code. However, the greatest
feature of Rails by far is Ruby! Throughout the book, we will explore
the power and productivity of Rails together. As you read, also be open
to Ruby itself, and be sure to absorb how Ruby forms the foundation of Rails.

PHP and Rails: A Personal View
Since the introduction of PHP 5, we’ve witnessed an evolution in PHP’s
capabilities. Perhaps even more than changes in PHP itself, we’ve seen
a transformation in the way programmers use it. Object-oriented programming
has become more commonplace. As a result, professional
software engineering practices such as unit testing have become more
practical and accessible to PHP developers.

We were early adopters of PHP 5. When Rails came along, we were already
sold on writing object-oriented, well-separated applications with
tests in PHP. Initially, we were a bit skeptical and didn’t have much
incentive to try Rails. We’re glad we did. We’re now excited about Rails
and enjoy building applications with it. We think you will as well, but
you’ll need to read the book and draw your own conclusions. We’ve
designed this book to be the guide that we wished we had when we
were in your shoes.

There are some software methodologies that we believe are applicable
to nearly all projects, such as the importance of object orientation and
unit testing. These opinions show in our writing. However, many other
factors determine how an application should be built and what tools
should be used. We believe that PHP, Ruby, and Rails are all just tools
you can choose from to build great applications.

It also might interest you to know that in our consulting practice, Maintainable
Software,3 we still develop roughly half of our new applications
in PHP 5 (with the other half being mostly Rails). This should tell you
that we think PHP is a formidable platform, and it’s not about “switching”
from one to the other. Learning Rails is just about having a new
tool to apply to your problems when it’s a good fit.

How to Read This Book
The goal of this book is to get you up to speed with both the Ruby
language and the Rails framework. To do this, we’ve divided the book
into three parts:
• Part I, “From PHP to Rails”
• Part II, “Building a Rails Application”
• Part III, “PHP to Ruby at a Glance”
The first part—“From PHP to Rails”—introduces the Model/View/Controller
pattern with the conversion of a simple PHP application to Rails.
This part then presents an introduction of Ruby to lay the foundation
for building a larger Rails application.

The second part—“Building a Rails Application”—guides you through
an in-depth application tutorial, from project conception all the way to
deployment. This part will cover the meat of building web applications
“the Rails way.”

The third and final part—“PHP to Ruby at a Glance”—provides an indepth
reference that maps PHP syntax and idioms to comparable Ruby
and Rails code. We provide one-to-one corresponding Ruby and PHP
code wherever possible to make the translation easy.

Both Ruby and Rails are invaluable development tools with their own
respective strengths and weaknesses. By the end of this book, you’ll
have a good understanding of both these tools and will be able to add
not only one but two new tricks to your development toolbox. Although
we’ll use PHP to drive our learning of Rails, Rails can help us learn
about PHP as well. As developers, we’re always on a quest to find faster
and more efficient ways to do our job. We hope that Rails inspires you to do just that.



Screenshot


Purchase Now !
Just with Paypal



Product details
 Price
 File Size
 12,980 KB
 Pages
 409 p
 File Type
 PDF format
 ISBN-10
 ISBN-13
 1-934356-04-2
 978-1-9343560-4-3
 Copyright
 2008 Derek DeVries and Mike Naberezny 


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
1 Getting Started with Rails
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
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
2.8 Handling Errors . . . . . . . . . . . . . . . . . . . . . . . 59
2.9 Understanding Blocks . . . . . . . . . . . . . . . . . . . 65
2.10 Chapter Review . . . . . . . . . . . . . . . . . . . . . . . 70
2.11 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
3 Embracing the Ruby Philosophy
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
4 Modeling the Domain
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
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
5.12 Chapter Review . . . . . . . . . . . . . . . . . . . . . . . 158
5.13 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
6 Validating and Testing Models
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
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 Logging In . . . . . . . . . . . . . . . . . . . . . . . . . . 195
7.6 Chapter Review . . . . . . . . . . . . . . . . . . . . . . . 200
7.7 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
8 Defining Associations
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
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 
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
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
11 PHP to Ruby Basics Reference 
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
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
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
Bibliography 399
Index 400

  ●▬▬▬▬▬❂❂❂▬▬▬▬▬●
●▬▬❂❂▬▬●
●▬❂▬●

═════ ═════

Previous Post Next Post