Ruby on Rails Bible. Wiley

In-dept coverage of the full-featured and easy-to-use Ruby on Rails framework

Timothy Fisher

Build and maintain Web sites and Application
Use Rail conventions to avoid redundant code
Learn advanced Rails techniques

E-books shop
Ruby on Rails Bible

About the Author
Timothy Fisher has over 17 years of experience in the software development industry. He has
served in a variety of roles including chief architect, technical team lead, and senior architect and
developer. Tim is currently an architect with the Compuware Corporation Professional Services
Group in Detroit, Michigan.

Ruby and the Ruby on Rails framework have consumed Tim’s interest and have led him to find the
Southeastern Michigan Ruby Users Group, as well as owning and maintaining the Michigan Ruby
Users Group Web site, www.rubymi.org. Tim is currently working on a large Ruby/Rails collaborative
project management and planning application to be released as an open source project in 2008.

Tim is an experienced technical writer and author who has contributed to Java developer’s Journal
and XML Journal and written the Java Phrasebook published by Pearson Ed. in 2006. In addition to
his technical skills Tim holds a degree in electrical engineering and a masters degree in education
with a specialty in instructional design for online learning. He lives in Flat Rock, Mich., with his
wife, Kerry, and two sons, Timmy and Camden.

Acknowledgements
It is impossible to write a book of this size without a great deal of help from those around me
and some that I’ve never even met face-to-face. First I’d like to thank Wiley for giving me the
opportunity to write this book. Writing a Rails book was something that I had wanted to do as
Rails has become a passion of mine the past few years. Specifically, I’d like to thank the people at
Wiley whom identified me as a candidate to write this book, and those that made the process
smooth and successful. Stephanie McComb is the acquisitions editor that gave me the opportunity
to be the author of this book. For that I am thankful. Chris Wolfgang served as my editor for this
book. Without her, I am quite certain the content you are about to read would not have been
nearly as clear and as readable as I hope it has become.

Ruby on Rails is a large framework that has been the subject of many books, many web sites, and
hundreds if not thousands of articles. There has been a great deal of knowledge and expertise
baked into the Rails framework. It would be impossible for a single person to write a comprehensive
Rails book without assistance others who review and provide feedback of the content. I’d like
to acknowledge the contributions of Scott Deming for his work as the Technical Editor of this
book. The job of a Technical Editor is at times more difficult than that of the author. It is the technical
editor’s job to review everything I have written and correct the mistakes and faulty knowledge
that may have passed through into the book. Scott’s advice and feedback have been invaluable in
creating this book.

The next person I’d like to thank also played a very large role in getting this book completed. That
person is Noel Rappin. Noel stepped in late in the writing phase and assisted with completing
some of the content. Noel contributed significant content to the following chapters: 5, 9, 10, 11,
and 12. Noel also wrote both of the appendixes for the book. Noel has his own book published by
Wrox, Professional Ruby on Rails.

Finally, I must acknowledge those who are closest to me, my family. Any author with a young
family appreciates the challenge of maintaining quality family life while writing a book. I have
two boys, Timmy and Camden, who like to keep their dad busy whenever they can. The time I
put into writing a book is time that has to come away from other tasks that I’d normally have more
time for. I thank my wife, Kerry, for her understanding of what it means to me to write this book
and her unwavering support and ability to help me find the time and effort to write. You can read
more about what interests me and perhaps learn a bit from my blog at http://blog.timothy
fisher.com. Now that this book is completed, I hope to become a much more active blogger!

Introduction

In 2006, I wrote a book called the Java Phrasebook, something like a cookbook for Java. While I
was writing that, my interest and love of Ruby and the Rails framework grew tremendously.
Often during my writing, I would think how much nicer it would be to be writing a Ruby- or
Rails-related book. Early in 2007, I had to pass up my first opportunity to step into the world of
Ruby and Rails writing. The book that I was asked to write at the time, Professional Ruby on Rails,
has since been written by a very capable writer, Noel Rappin, who also contributed content to this book.

Jump ahead a few months, and the opportunity to write this book, Ruby on Rails Bible, came along.
I knew it would be a tight fit working on this book along with a full-time job and the holidays
coming up, especially having two young children, but I took it! That is how you ended up holding
this book now.

Like many who consider themselves users and, more importantly, fans of the Ruby language, I was
pulled into the world of Ruby by the Rails framework. Prior to Rails, I had heard of Ruby but had
not used it. I first became aware of it through the writing and speaking of Dave Thomas, a tireless
advocate of Ruby well before Rails made it a marketable skill. Ruby had been around for quite
awhile before Rails but had not been able to grab the attention of the masses here in the United
States. Rails has not only brought Ruby to the masses, but it has had a tremendous influence on
the entire Web development industry. Rails clones have sprung up in many languages, including
Java, Perl, Python, and PHP. Many of the patterns and methods of Rails have influenced other
frameworks in other languages as well.

By the time you read this book, you’ll have a choice of many books on the subject of Ruby on
Rails. I hope that you find this book was worth your energy!

Who the book is for
This book is for any Web developers who are interested in learning how to create Web applications
using the Ruby on Rails framework. You do not have to know Ruby to use this book. In part
I of the book, you can get an introduction to Ruby and learn enough about it to effectively create
basic Rails applications. You should have some experience with common Web development technologies
such as HTML, JavaScript, and preferably some server-side language such as Java, .net,
Perl, PHP, Ruby, or any other language that you might use to write the server-side of a Web application.
Although not required, basic knowledge of DOM and CSS would also prove helpful as you
write Rails applications shown in this book.

How the book is organized
This book is organized into five main parts:
Part I: First Steps with Rails
Part II: Rails In Depth
Part III: Developing a Complete Rails Application
Part IV: Advanced Rails
Part V: Appendixes
Each of these parts is broken down into several chapters.

Part I
This part of the book will teach you the underpinnings that you need to effectively develop a Ruby
on Rails application. You can learn the basics of Ruby and get your first introduction to the Rails
framework.
Part II
After you’ve been exposed to the basics of Ruby and Rails, you can immerse yourself in the details
of each of the main components that make up the Rails framework, the Model, Controller, and
View layers.
Part III
I hope you’ll enjoy reading and following along with this part of the book as much as I enjoyed
writing it! In this part, you can follow along with the development of a complete Rails application.
You’ll go from nothing up to a usable application that you can use within any group or organization
to share information about a collection of books.
Part IV
This part of the book covers more advanced Rails topics, such as extending Rails through plugins,
generators, and engines. You’ll also get an introduction to the Prototype and Scriptaculous
JavaScript libraries in this part.
Part V
If you need extra resources on the Ruby language or references for Rails, these appendixes can offer
you a quick place to look up facts in a hurry.

How to use this book
This book is organized such that it can be read from cover to cover. If you’re a new Rails developer
reading it from cover to cover is the best way to learn about Rails. If you are new to Rails but know
Ruby already, you can skip the Ruby introduction and just read the chapters that discuss Rails. As
you read through the book, your knowledge of Rails will build with each chapter.

If you know Rails already, you may want to skip ahead to Chapter 6 and read about the development
of the Book Shelf Web application. I think you’ll find the development of that application
will interest even a seasoned Rails developer.

After you’ve read the book, it is also suitable as a Rails reference that you’ll want to keep within
reach on your bookshelf. The two appendixes at the end of the book provide a thorough reference
to both the Ruby language and the Rails API.



Screenshot

E-books Shop

Purchase Now !
Just with Paypal



Product details
 Price
 File Size
 10,006 KB
 Pages
 627 p
 File Type
 PDF format
 ISBN
 978-0-470-25822-4
 Copyright
 2008 by Wiley Publishing, Inc  

Contents at a Glace
Part I: First Steps with Rails
Chapter 1: Learning Ruby
Chapter 2: Getting Started with Rails
Part II: Rails In Depth
Chapter 3: Using Active Record
Chapter 4: Controller: In Depth
Chapter 5: View: In Depth
Part III: Developing a Complete Rails Application
Chapter 6: Developing Book Shelf: The Basic Framework
Chapter 7: Developing Book Shelf: Adding the Core Functionality
Chapter 8: Developing Book Shelf: Social Support
Chapter 9: Testing the Book Shelf Application
Part IV: Advanced Rails
Chapter 10: Using Prototype and script.aculo.us
Chapter 11: Extending Rails
Chapter 12: Advanced Topics
Part V: Appendixes
Appendix A: Ruby Quick Reference
Appendix B: Ruby on Rails Guide
Index


Table of Contents

Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi
Ruby on Rails Quick Start . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1
Installing Instant Rails on Windows ......................................................................................2
Installing Ruby and Rails on Mac OS X and Linux ................................................................6
Installing on Linux ......................................................................................................6
Installing on Mac OS X................................................................................................7
Setting up a Development Environment ................................................................................7
Source code version control ........................................................................................7
IDE or Editor? .............................................................................................................8
TextMate ...........................................................................................................8
E .....................................................................................................................9
IntelliJ IDEA ......................................................................................................9
NetBeans ...........................................................................................................9
Eclipse .............................................................................................................10
Heroku ............................................................................................................10
Aptana Studio ..................................................................................................11
Summary ............................................................................................................................11
Part I: First Steps with Rails 13
Chapter 1: Learning Ruby . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
The Nature of Ruby ............................................................................................................15
Dynamic or static typing ...........................................................................................16
Duck typing ..............................................................................................................16
Compiled or scripting language .................................................................................17
Compiled languages ........................................................................................17
Scripted languages ...........................................................................................17
Object Oriented Programming ............................................................................................18
The Basics of Ruby ..............................................................................................................19
Ruby’s interactive shell ..............................................................................................19
Ruby syntax basics ....................................................................................................20
Adding comments ...........................................................................................20
Using parentheses ............................................................................................21
Using white space ............................................................................................22
Using semicolons .............................................................................................22
Running Ruby programs ...........................................................................................23
Classes, Objects, and Variables ...........................................................................................24
Using objects in Ruby................................................................................................24
Defining objects ...............................................................................................24
Writing methods .............................................................................................25
Methods with parameters ................................................................................26
Creating instances of a class .............................................................................27
Instance and class methods .............................................................................28
Instance and class variables .............................................................................30
Getters and setters in Ruby objects ..................................................................32
Inheritance ................................................................................................................36
Built-in Classes and Modules ..............................................................................................37
Scalar objects ............................................................................................................38
Strings .............................................................................................................38
Numerics .........................................................................................................41
Symbols ...........................................................................................................42
Times and dates ...............................................................................................42
Collections ................................................................................................................47
Arrays ..............................................................................................................47
Hashes .............................................................................................................48
Ranges .............................................................................................................49
Control Flow ......................................................................................................................51
Conditionals ..............................................................................................................51
The if statement ...............................................................................................52
The unless statement .......................................................................................52
The case statement ..........................................................................................53
Loops, blocks, and iterators .......................................................................................53
for loops ..........................................................................................................54
while and until loops .......................................................................................54
Blocks ..............................................................................................................55
Iterators ...........................................................................................................56
Exception handling ...................................................................................................57
Exceptions in Ruby ..........................................................................................57
Using begin, raise, and rescue..........................................................................58
More exception handling using ensure, retry, and else ....................................60
Organizing Code with Modules...........................................................................................62
Advanced Ruby Techniques ................................................................................................63
Variable length argument lists ...................................................................................63
Dynamic programming with method_missing ...........................................................64
Reopening classes ......................................................................................................65
Summary ............................................................................................................................65
Chapter 2: Getting Started with Rails . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
What is Ruby on Rails? .......................................................................................................68
DRY ..........................................................................................................................69
Convention over configuration ..................................................................................69
Opinionated software ................................................................................................69
Rails Architecture ................................................................................................................70
MVC .........................................................................................................................70
Rails and MVC ..........................................................................................................71
Rails Scripts ........................................................................................................................73
Rails Console .............................................................................................................73
WEBrick....................................................................................................................74
Generators.................................................................................................................74
Migrations .................................................................................................................74
Your First Rails Application ................................................................................................74
Create the project ......................................................................................................75
Set up the database....................................................................................................77
Create the model .......................................................................................................79
Create the controller and views .................................................................................82
Implementing the index action ........................................................................85
Implementing the new action ..........................................................................89
Implementing the create action .......................................................................91
Implementing the show action ........................................................................92
Implementing the update action ......................................................................94
What you have accomplished ..........................................................................95
Style the application ..................................................................................................96
WebScaffolding .........................................................................................................98
More to get you started ...........................................................................................102
Summary ..........................................................................................................................103
Part II: Rails In Depth 105
Chapter 3: Using Active Record . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
What is Active Record? .....................................................................................................108
Active Record Basics .........................................................................................................109
Active Record classes and objects ............................................................................109
Active Record naming conventions ..........................................................................109
Class and table names ....................................................................................110
Table keys .....................................................................................................110
Setting up a Model ............................................................................................................112
Generate a Rails project and model .........................................................................112
Configure Active Record..........................................................................................113
Rails Development Environments ............................................................................114
Using Migrations ...............................................................................................................115
Schema versions ......................................................................................................118
Migration methods ..................................................................................................119
Tables ............................................................................................................119
Columns ........................................................................................................120
Indexes ..........................................................................................................120
Inserting data with migrations .................................................................................120
Create, Read, Update, and Delete ......................................................................................122
Creating records ......................................................................................................122
Reading data ...........................................................................................................124
Column metadata ..........................................................................................124
Accessing attributes .......................................................................................125
Using the find method ...................................................................................126
Dynamic finders ............................................................................................127
Find using SQL .............................................................................................128
Creating and finding data with the Rails Console ...................................................128
Updating records.....................................................................................................130
Deleting records ......................................................................................................130
Using development log files.....................................................................................130
Defining Relationships ......................................................................................................131
One-to-one relationships .........................................................................................132
Methods added by has_one ...........................................................................133
Methods added by belongs_to .......................................................................134
Many-to-one relationships .......................................................................................134
Methods added by has_many ........................................................................135
Methods added by belongs_to .......................................................................136
Many-to-many relationships ....................................................................................136
Methods added by has_and_belongs_to_many ..............................................137
Implementing Validations .................................................................................................139
Custom Validations ...........................................................................................................141
Advanced Active Record ...................................................................................................142
Single table inheritance ...........................................................................................142
Composition ...........................................................................................................144
Transactions ............................................................................................................145
Summary ..........................................................................................................................146
Chapter 4: Controller: In Depth . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
What is ActionController? .................................................................................................147
All About Routing .............................................................................................................148
Defining custom routes ...........................................................................................149
Defining a custom default route ..............................................................................151
Using named routes ................................................................................................152
Constructing URLs with url_for ..............................................................................153
Creating and Using Controllers .........................................................................................155
Generating controllers .............................................................................................155
Action methods .......................................................................................................157
Using request parameters ..............................................................................158
Rendering templates ......................................................................................159
Redirects .......................................................................................................160
Sending feedback with flash ..........................................................................160
Sending other types of data to the browser ....................................................162
Using Filters ......................................................................................................................163
Before filters ............................................................................................................163
After filters ..............................................................................................................164
Around filters ..........................................................................................................164
Protecting filter methods .........................................................................................166
Working with Sessions ......................................................................................................167
Using the ActiveRecord session storage ...................................................................168
Using MemCached session storage ..........................................................................169
Caching ............................................................................................................................169
Page caching ...........................................................................................................170
Action caching ........................................................................................................171
Fragment caching ....................................................................................................171
Summary ..........................................................................................................................172
Chapter 5: View: In Depth . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
ActionView .......................................................................................................................173
Getting to the view ..................................................................................................174
Rendering options ...................................................................................................175
Responding to different formats ..............................................................................176
Embedded Ruby (ERb) .....................................................................................................179
Using the <%- and -%> delimiters ...........................................................................180
Commenting out embedded Ruby ...........................................................................181
Layouts .............................................................................................................................182
Partials ..............................................................................................................................185
Helpers .............................................................................................................................186
Predefined Rails helpers ..........................................................................................187
HTML creation helpers ..................................................................................188
Form creation helpers ....................................................................................190
JavaScript creation helpers .............................................................................192
Data Processing helpers .................................................................................193
Debugging helpers .........................................................................................197
Creating your own block helpers .............................................................................197
JavaScript, Ajax, and RJS ...................................................................................................198
Prototype helpers ....................................................................................................198
RJS helpers ..............................................................................................................200
Summary ..........................................................................................................................202
Part III: Developing a Complete Rails Application 203
Chapter 6: Developing Book Shelf: The Basic Framework . . . . . . . . . . . 205
Application Overview .......................................................................................................206
Creating a Skeleton for the Book Shelf Application ...........................................................211
Begin the Book Shelf project ....................................................................................211
Setting up the databases ..........................................................................................213
Create the databases ......................................................................................213
Create a Home Page ..........................................................................................................214
Create the Home controller .....................................................................................215
Create a layout and view .........................................................................................216
The HTML head ............................................................................................217
The body header section ................................................................................219
The body sidebar section ...............................................................................219
The body content section ..............................................................................220
Creating the index view template ..................................................................221
Testing the home page ...................................................................................221
Add some style ..............................................................................................222
Set up a default route ..............................................................................................225
Implementing Users ..........................................................................................................225
Create the user model .............................................................................................226
Securing user passwords ................................................................................227
Generate the user model ................................................................................227
Create the user migration ..............................................................................228
Add user model validations ...........................................................................229
Implement user registration ....................................................................................232
Create a user controller .................................................................................232
Handle the user password .............................................................................234
Create a registration view ..............................................................................235
Create user home view ..................................................................................238
Implement login and logout ....................................................................................240
Create login action method............................................................................241
Create the logout action method ....................................................................242
Create an application login partial .................................................................242
Test the login and logout functionality ..........................................................245
Using a before filter to protect pages .......................................................................246
Modify the application controller ..................................................................247
Add login form to the signup page ................................................................248
Summary ..........................................................................................................................248
Chapter 7: Developing Book Shelf: Adding the Core Functionality . . . . 249
Adding Support for Books .................................................................................................249
Refactor the Sidebar Code .................................................................................................250
Integrating with Amazon ...................................................................................................252
Install Ruby/Amazon ...............................................................................................254
Implement the Book Shelf-Amazon interface ...........................................................256
The initialize method .....................................................................................256
The find_by_keyword method ......................................................................257
Implementing a Search .....................................................................................................260
Create the book search form ...................................................................................261
Generate the book controller and search action .......................................................264
Create the book model ............................................................................................264
Generate the book model class ......................................................................265
Create the book migration .............................................................................265
Run the migration .........................................................................................266
Associate the book model and the user model ...............................................266
Implement search logic in the book model ..............................................................267
Create the search results page ..................................................................................269
Implement search results paging .............................................................................276
Implementing the Addition and Deletion of Books ............................................................279
Adding a book.........................................................................................................279
Deleting a book .......................................................................................................284
Add the delete action .....................................................................................284
Update the page with RJS ..............................................................................285
Displaying a User’s Books .................................................................................................286
Implementing the Book Detail Page ..................................................................................287
Summary ..........................................................................................................................290
Chapter 8: Developing Book Shelf: Social Support . . . . . . . . . . . . . . . . 291
Adding Social Support ......................................................................................................291
Implementing Tagging ......................................................................................................292
Installing a tagging plugin .......................................................................................292
Install the acts_as_taggable_redux plugin ......................................................293
Setting up the database for tagging support .............................................................294
Adding tagging support to the models .....................................................................297
Adding tagging support to the controllers ...............................................................298
Creating the view layer for tagging ..........................................................................301
Implementing the sidebar tag cloud view ......................................................303
Implement tag_cloud_revised helper method ................................................307
Generate the tagging style sheet .....................................................................310
Implement the static tag view ........................................................................311
Make the tags editable ...................................................................................313
Implement the show_for_tag view .................................................................314
Using tags ...............................................................................................................314
Implementing Book Reviews .............................................................................................314
Implementing the review model ..............................................................................315
Adding associations to the book and user models ...................................................317
Implementing the review view ................................................................................317
Displaying reviews for a book ........................................................................318
Implementing the review entry form .............................................................323
Implementing the review controller ........................................................................324
Adding some style ...................................................................................................326
Adding a book review .............................................................................................327
Implementing Book Ratings ..............................................................................................328
Extending the Application .................................................................................................329
Improving the user interface....................................................................................329
Implementing an administrator interface .................................................................329
Adding RSS feeds to the application ........................................................................330
Adding support for other book information souces .................................................330
Implementing recommendations and suggestions ...................................................330
Expanding user profiles and adding avatars .............................................................330
Summary ..........................................................................................................................331
Chapter 9: Testing the Book Shelf Application . . . . . . . . . . . . . . . . . . . . 333
Why Test? .........................................................................................................................333
Using Test::Unit ................................................................................................................336
Test assertions .........................................................................................................336
Test fixtures ............................................................................................................338
Test methods ...........................................................................................................338
Test runners ............................................................................................................340
Test suites ...............................................................................................................341
Testing in Rails .................................................................................................................342
Rails test directory ...................................................................................................342
Rails test lifecycle ....................................................................................................343
Setting up a Test Database ................................................................................................343
Functional Tests ................................................................................................................345
Running a test .........................................................................................................347
Creating a test .........................................................................................................348
Setting up fixtures .........................................................................................349
Writing a test method ....................................................................................351
More assertion methods .................................................................................353
Verifying your test .........................................................................................356
Adding more tests ...................................................................................................356
Add some reviews .........................................................................................359
Verify the tests again ......................................................................................360
Unit Tests .........................................................................................................................361
Setting up user fixtures............................................................................................363
Test authentication ..................................................................................................363
Test validations .......................................................................................................365
Test valid password .......................................................................................365
Test valid login ..............................................................................................367
Test valid e-mail ............................................................................................368
Integration Tests ...............................................................................................................370
Running All Tests ..............................................................................................................374
Test Coverage ...................................................................................................................374
Debugging Techniques ......................................................................................................375
The Rails log files ....................................................................................................375
Console-based .........................................................................................................376
Using the debugger .................................................................................................376
Summary ..........................................................................................................................377
Part IV: Advanced Rails 379
Chapter 10: Using Prototype and script.aculo.us . . . . . . . . . . . . . . . . . . 381
Prototype, script.aculo.us, and Rails ..................................................................................382
Using Prototype and script.aculo.us from Rails .......................................................382
Create a Rails project ...............................................................................................383
Include the Prototype and script.aculo.us files ...............................................383
Prototype Overview ..........................................................................................................384
Ruby’s influence on Prototype .................................................................................385
What is Prototype? ..................................................................................................385
Extensions to JavaScript ....................................................................................................385
Simplifying JavaScript with the dollar sign ..............................................................386
Selecting elements with $ ..............................................................................386
Selecting elements with $$ ............................................................................387
Creating arrays with $A .................................................................................388
Splitting strings with $w ................................................................................389
Getting form field values with $F ..................................................................389
Creating hashes with $H ...............................................................................389
Creating ranges with $R ................................................................................389
More powerful arrays ..............................................................................................389
Enumerating an array ....................................................................................390
JSON support ..........................................................................................................391
OOP with Prototype .........................................................................................................392
Defining classes and inheritance ..............................................................................392
Implementing class inheritance with Prototype .......................................................393
Event Handling .................................................................................................................393
Ajax ..................................................................................................................................394
Ajax links ................................................................................................................394
Ajax link options .....................................................................................................395
script.aculo.us Overview ...................................................................................................396
Visual Effects ....................................................................................................................396
script.aculo.us effects ..............................................................................................397
Effect options ..........................................................................................................398
Using combination effects .......................................................................................399
Controls ............................................................................................................................399
Sliders .....................................................................................................................399
Auto-completion .....................................................................................................401
In-place editing .......................................................................................................402
Implementing a single value in-place editor ...................................................403
Drag and Drop ..................................................................................................................406
Creating draggable elements ....................................................................................406
Draggable options ..........................................................................................407
Creating droppable elements ...................................................................................409
Droppable options .........................................................................................409
Sortable lists ............................................................................................................409
JavaScript Testing .............................................................................................................411
Creating JavaScript unit tests ...................................................................................411
Running JavaScript unit tests ...................................................................................413
Summary ..........................................................................................................................413
Chapter 11: Extending Rails . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 415
Beyond the Core ...............................................................................................................415
Generators ........................................................................................................................416
The generator directory structure ............................................................................417
Writing generator code............................................................................................418
Creating the templates .............................................................................................421
Running the new authentication generator ..............................................................424
Extending Rails::Generator::NamedBase ..................................................................425
Plugins ..............................................................................................................................425
Using the Plugin script ............................................................................................426
List available plugins .....................................................................................427
List plugin sources .........................................................................................427
Adding and removing plugin sources ............................................................428
Discover new plugin sources .........................................................................428
Installing, removing, and updating plugins....................................................429
Writing a plugin ......................................................................................................430
Write a new plugin ........................................................................................431
Try out the new plugin ..................................................................................433
Common techniques used to develop plugins .........................................................435
Extending classes with mixins .......................................................................435
Opening a class .............................................................................................437
Dynamic extension with callbacks and hooks ................................................437
Using code generation ...................................................................................439
Managing plugins with Piston .................................................................................439
Summary of Useful Plugins ...............................................................................................440
acts_as_rateable.......................................................................................................441
Pagination ...............................................................................................................442
Installing will_paginate ..................................................................................442
Adding pagination to your application ..........................................................442
acts_as_state_machine.............................................................................................444
Installing acts_as_state_machine ...................................................................444
Using acts_as_state_machine .........................................................................444
annotate_models .....................................................................................................446
Installing annotate_models ............................................................................446
Using annotate_models .................................................................................446
exception_notifier ...................................................................................................447
Installing exception_notifier ..........................................................................448
Using exception_notifier ................................................................................448
resource_controller .................................................................................................449
Installing resoure_controller ..........................................................................449
Using resource_controller ..............................................................................449
Adding user authentication .....................................................................................451
Installing restful_authentication ....................................................................451
Using restful_authentication ..........................................................................452
Enhanced scaffolding ..............................................................................................452
Streamlined ...................................................................................................453
ActiveScaffold ................................................................................................458
Implementing content tagging .................................................................................460
Handling file uploads ..............................................................................................461
attachment_fu ...............................................................................................461
Engines .............................................................................................................................463
Install the Engines plugin ........................................................................................464
Generate the engine skeleton ...................................................................................464
Move your application files into the engine .............................................................464
Modify your environment .......................................................................................465
Include your engine in your application ..................................................................465
Summary ..........................................................................................................................465
Chapter 12: Advanced Topics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 467
Beyond the Basics .............................................................................................................467
RESTful Rails ....................................................................................................................468
Some advantages of RESTful architecture ................................................................470
REST as a Web service architecture ........................................................................471
REST and representations .......................................................................................471
Writing a RESTful application with Rails .................................................................472
Rails routing and REST ..................................................................................472
PUT and DELETE full disclosure ...................................................................473
Generate a RESTful resource..........................................................................474
Working with Legacy Databases ........................................................................................481
Override database table and field names ..................................................................482
Side by side with the legacy database ......................................................................484
Using ActionMailer ...........................................................................................................487
Configuring a Rails application for e-mail support ..................................................488
Generating a mailer model ......................................................................................488
Writing code to send e-mail ...................................................................................489
Writing code to receive e-mail ................................................................................491
ActiveResource and XML ..................................................................................................492
Deploying with Capistrano ...............................................................................................494
Installing and setting up Capistrano ........................................................................495
Running basic capistrano tasks ................................................................................496
Customizing Capistrano ..........................................................................................499
Summary ..........................................................................................................................500
Part V: Appendixes 503
Appendix A: Ruby Quick Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . 505
Basic Ruby Syntax .............................................................................................................505
Literal expressions ...................................................................................................506
Arrays ............................................................................................................506
Boolean literals ..............................................................................................507
Hashes ...........................................................................................................507
Numbers .......................................................................................................508
Ranges ...........................................................................................................508
Regular expressions .......................................................................................508
Strings ...........................................................................................................510
Symbols .........................................................................................................511
Variable and method names ....................................................................................512
Operators ................................................................................................................513
Method calls ............................................................................................................515
Special keyword expressions ...................................................................................516
The if expression ...........................................................................................516
The unless expression ....................................................................................518
The case expression .......................................................................................518
The for expression .........................................................................................519
The while expression .....................................................................................520
The until expression ......................................................................................521
Loop control keywords ..................................................................................521
Assignment .............................................................................................................521
File input and output ..............................................................................................522
Exceptions ..............................................................................................................523
Objects and Classes ...........................................................................................................524
Defining methods ....................................................................................................524
Blocks .....................................................................................................................526
Defining classes and modules ..................................................................................527
Defining modules ..........................................................................................527
Defining classes .............................................................................................528
Superclasses and self .....................................................................................529
Including and extending with modules .........................................................529
Attributes .....................................................................................................531
Access control ...............................................................................................531
Method lookup .......................................................................................................532
Appendix B: Ruby on Rails Guide . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 535
Getting Started ..................................................................................................................535
Standard Rails application .......................................................................................536
Generators...............................................................................................................538
Controllers and Helpers ....................................................................................................540
Traditional routing ..................................................................................................540
RESTful routing ......................................................................................................541
Controller variables .................................................................................................543
Filters ......................................................................................................................544
Rendering and redirecting .......................................................................................544
Respond to ..............................................................................................................546
Helpers ...................................................................................................................547
Views ...............................................................................................................................548
ERb .........................................................................................................................548
RJS ..........................................................................................................................549
Models ..............................................................................................................................550
Creating ..................................................................................................................551
Reading ...................................................................................................................553
Updating .................................................................................................................554
Deleting ..................................................................................................................555
Relationships ...........................................................................................................555
belongs_to .....................................................................................................556
has_one .........................................................................................................557
has_many ......................................................................................................557
has_and_belongs_to_many ...........................................................................559
Database Migrations ..........................................................................................................560
Plugins ..............................................................................................................................562
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 565

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

═════ ═════

Previous Post Next Post