The Ruby way solutions and techniques in Ruby programming. - Includes index [2nd ed]
0672328844, 9780672328848
Ruby is an agile object-oriented language, borrowing some of the best features from LISP, Smalltalk, Perl, CLU, and othe
295
55
4MB
English
Pages 839
[911]
Year 2006
Report DMCA / Copyright
DOWNLOAD PDF FILE
Table of contents :
Contents......Page 11
Chapter 1 Rails Environments and Configuration......Page 60
Default Environment Settings......Page 61
Bootstrapping......Page 62
RubyGems......Page 64
Default Load Paths......Page 65
Rails, Modules, and Auto-Loading Code......Page 66
Builtin Rails Info......Page 67
Configuration......Page 68
Additional Configuration......Page 72
Development Mode......Page 73
The Rails Class Loader......Page 74
Production Mode......Page 76
Logging......Page 77
Rails Log Files......Page 79
Log File Analysis......Page 81
Syslog......Page 83
References......Page 84
Chapter 2 Working with Controllers......Page 86
Request Handling......Page 87
Getting Intimate with the Dispatcher......Page 88
Render unto View.........Page 90
When in Doubt, Render......Page 91
Rendering Another Action's Template......Page 92
Rendering a Different Template Altogether......Page 93
Rendering Text......Page 94
Rendering Nothing......Page 95
Rendering Options......Page 96
Redirecting......Page 98
Controller/View Communication......Page 101
Filters......Page 102
Filter Inheritance......Page 103
Filter Types......Page 105
Filter Chain Ordering......Page 106
Around Filters......Page 107
Filter Chain Skipping......Page 108
Filter Chain Halting......Page 109
send_data(data, options = {})......Page 110
send_file(path, options = {})......Page 111
Letting the Web Server Send Files......Page 114
References......Page 115
Chapter 3 Routing......Page 116
The Two Purposes of Routing......Page 117
Bound Parameters......Page 119
Wildcard Components ("Receptors")......Page 120
Static Strings......Page 121
The routes.rb File......Page 122
The Default Route......Page 124
Spotlight on the :id Field......Page 125
Default Route Generation......Page 126
Modifying the Default Route......Page 127
The Ante-Default Route and respond_to......Page 128
respond_to and the HTTP-Accept Header......Page 129
The Empty Route......Page 130
Using Static Strings......Page 131
Using Your Own "Receptors"......Page 132
A Note on Route Order......Page 134
Default Parameters and the url_for Method......Page 135
What Happened to :id?......Page 136
Route Globbing......Page 138
Globbing Key-Value Pairs......Page 139
Creating a Named Route......Page 140
The Question of Using name_path Versus name_url......Page 141
What to Name Your Routes......Page 142
Argument Sugar......Page 143
A Little More Sugar with Your Sugar?......Page 144
The Special Scope Method with_options......Page 145
References......Page 147
REST in a Rather Small Nutshell......Page 148
REST in Rails......Page 150
Routing and CRUD......Page 151
REST Resources and Rails......Page 152
From Named Routes to REST Support......Page 153
The Standard RESTful Controller Actions......Page 155
Singular and Plural RESTful Routes......Page 157
The Special Pairs: new/create and edit/update......Page 158
Singular Resource Routes......Page 159
Nested Resources......Page 160
Setting :name_prefix Explicitly......Page 162
All Together Now......Page 164
Considerations......Page 166
Deep Nesting?......Page 167
Extra Member Routes......Page 169
Extra Collection Routes......Page 170
Considerations......Page 171
Controller-Only Resources......Page 172
Different Representations of Resources......Page 174
The respond_to Method......Page 175
The RESTful Rails Action Set......Page 176
Index......Page 177
Destroy......Page 180
New and Create......Page 182
Edit and Update......Page 183
Conclusion......Page 184
Reference......Page 185
Examining Routes in the Application Console......Page 186
Dumping Routes......Page 187
Anatomy of a Route Object......Page 188
Recognition and Generation in the Console......Page 191
Named Routes in the Console......Page 193
Testing Routes......Page 194
The Routing Navigator Plugin......Page 195
References......Page 196
Chapter 6 Working with ActiveRecord......Page 198
The Basics......Page 199
Migrations......Page 201
Creating Migrations......Page 202
Migration API......Page 205
Defining Columns......Page 208
Relationship Declarations......Page 214
Convention over Configuration......Page 215
Pluralization......Page 216
Setting Names Manually......Page 217
Legacy Naming Schemes......Page 218
Default Attribute Values......Page 219
Serialized Attributes......Page 221
Creating New ActiveRecord Instances......Page 222
Reading ActiveRecord Objects......Page 223
Reading and Writing Attributes......Page 225
Accessing and Manipulating Attributes Before They Are Typecast......Page 227
Dynamic Attribute-Based Finders......Page 228
Custom SQL Queries......Page 230
The Query Cache......Page 231
Updating......Page 233
Updating a Particular Instance......Page 235
Convenience Updaters......Page 236
Deleting and Destroying......Page 237
Database Locking......Page 238
Optimistic Locking......Page 239
Pessimistic Locking......Page 241
Conditions......Page 242
Limit and Offset......Page 245
Select Option......Page 246
Group By Option......Page 247
Connections to Multiple Databases in Different Models......Page 248
The DatabaseStatements Module......Page 250
Other Connection Methods......Page 253
Other Configuration Options......Page 255
Conclusion......Page 256
References......Page 257
The Association Hierarchy......Page 258
One-to-Many Relationships......Page 260
Adding Associated Objects to a Collection......Page 262
AssociationCollection Methods......Page 263
The belongs_to Association......Page 266
Building and Creating Related Objects via the Association......Page 267
belongs_to Options......Page 268
The has_many Association......Page 274
has_many Options......Page 275
Proxy Methods......Page 283
has_and_belongs_to_many......Page 284
has_many :through......Page 292
has_many :through Options......Page 297
has_one......Page 300
has_one Options......Page 303
One-to-One Associations......Page 305
Association Extensions......Page 306
The AssociationProxy Class......Page 308
Conclusion......Page 309
References......Page 310
Finding Errors......Page 312
validates_acceptance_of......Page 313
validates_associated......Page 314
validates_each......Page 315
validates_existence_of......Page 316
validates_format_of......Page 317
validates_length_of......Page 318
validates_numericality_of......Page 319
validates_uniqueness_of......Page 320
:message......Page 322
Conditional Validation......Page 323
Usage and Considerations......Page 324
Working with the Errors Object......Page 325
Checking for Errors......Page 326
Custom Validation......Page 327
Skipping Validations......Page 328
Reference......Page 329
Callbacks......Page 330
Callback Registration......Page 331
Halting Execution......Page 333
Callback Usages......Page 334
Special Callbacks: after_initialize and after_find......Page 337
Callback Classes......Page 338
Observers......Page 341
Registration of Observers......Page 342
Single-Table Inheritance (STI)......Page 343
Mapping Inheritance to the Database......Page 345
STI and Associations......Page 347
Abstract Base Model Classes......Page 350
Polymorphic has_many Relationships......Page 351
In the Case of Models with Comments......Page 352
Modules for Reusing Common Behavior......Page 355
A Review of Class Scope and Contexts......Page 358
The included Callback......Page 359
Modifying ActiveRecord Classes at Runtime......Page 360
Ruby and Domain-Specific Languages......Page 362
References......Page 364
Chapter 10 ActionView......Page 366
ERb Practice......Page 367
Commenting Out ERb Delimiters......Page 369
RHTML? RXML? RJS?......Page 370
Layouts and Templates......Page 371
Yielding Content......Page 372
Template Variables......Page 374
Protecting the Integrity of Your View from User-Submitted Content......Page 378
Partials......Page 379
Simple Use Cases......Page 380
Reuse of Partials......Page 381
Shared Partials......Page 382
Passing Variables to Partials......Page 383
Render Collections......Page 384
Caching......Page 386
Action Caching......Page 387
Fragment Caching......Page 389
Expiration of Cached Content......Page 392
Automatic Cache Expiry with Sweepers......Page 394
Action Cache Plugin......Page 396
Cache Storage......Page 397
References......Page 399
Chapter 11 All About Helpers......Page 400
Reporting Validation Errors......Page 401
Automatic Form Creation......Page 403
Customizing the Way Validation Errors Are Highlighted......Page 405
Head Helpers......Page 407
For Plugins Only, Add Default JavaScript Includes......Page 411
CacheHelper......Page 412
CaptureHelper......Page 413
The Date and Time Selection Helpers......Page 414
The Individual Date and Time Select Helpers......Page 415
distance_in_time Methods with Complex Descriptive Names......Page 418
DebugHelper......Page 420
Creating Forms for ActiveRecord Models......Page 421
How Form Helpers Get Their Values......Page 429
Select Helpers......Page 430
Option Helpers......Page 432
FormTagHelper......Page 437
JavaScriptHelper......Page 440
NumberHelper......Page 442
will_paginate......Page 445
paginator......Page 446
RecordIdentificationHelper......Page 447
RecordTagHelper......Page 449
TagHelper......Page 450
TextHelper......Page 452
UrlHelper......Page 459
Small Optimizations: The Title Helper......Page 466
Encapsulating View Logic: The photo_for Helper......Page 467
Smart View: The breadcrumbs Helper......Page 468
A tiles Helper......Page 469
Generalizing Partials......Page 473
References......Page 476
Chapter 12 Ajax on Rails......Page 478
Prototype......Page 479
The Prototype API......Page 480
Top-Level Functions......Page 481
Class......Page 483
Extensions to JavaScript's Object Class......Page 484
Extensions to JavaScript's Array Class......Page 485
Extensions to the Event Class......Page 487
Extensions to JavaScript's Function Class......Page 489
Extensions to JavaScript's Number Class......Page 491
Extensions to JavaScript's String class......Page 492
The Ajax Object......Page 495
Enumerable......Page 496
Hash......Page 502
ObjectRange......Page 503
link_to_remote......Page 504
remote_form_for......Page 508
observe_field......Page 510
RJS—Writing Javascript in Ruby......Page 512
RJS Templates......Page 514