The Rails Way

The Rails Way

3.81 (261 ratings by Goodreads)
By (author) 

List price: US$49.99

Currently unavailable

Add to wishlist

AbeBooks may have this title (opens in new window).

Try AbeBooks

Description

The expert guide to building Ruby on Rails applications



Ruby on Rails strips complexity from the development process, enabling professional developers to focus on what matters most: delivering business value. Now, for the first time, there's a comprehensive, authoritative guide to building production-quality software with Rails. Pioneering Rails developer Obie Fernandez and a team of experts illuminate the entire Rails API, along with the Ruby idioms, design approaches, libraries, and plug-ins that make Rails so valuable. Drawing on their unsurpassed experience, they address the real challenges development teams face, showing how to use Rails' tools and best practices to maximize productivity and build polished applications users will enjoy.



Using detailed code examples, Obie systematically covers Rails' key capabilities and subsystems. He presents advanced programming techniques, introduces open source libraries that facilitate easy Rails adoption, and offers important insights into testing and production deployment. Dive deep into the Rails codebase together, discovering why Rails behaves as it does- and how to make it behave the way you want it to.



This book will help you

Increase your productivity as a web developer

Realize the overall joy of programming with Ruby on Rails

Learn what's new in Rails 2.0

Drive design and protect long-term maintainability with TestUnit and RSpec

Understand and manage complex program flow in Rails controllers

Leverage Rails' support for designing REST-compliant APIs

Master sophisticated Rails routing concepts and techniques

Examine and troubleshoot Rails routing

Make the most of ActiveRecord object-relational mapping

Utilize Ajax within your Rails applications

Incorporate logins and authentication into your application

Extend Rails with the best third-party plug-ins and write your own

Integrate email services into your applications with ActionMailer

Choose the right Rails production configurations

Streamline deployment with Capistrano
show more

Product details

  • Paperback | 912 pages
  • 232 x 235 x 51mm | 1,408g
  • Addison-Wesley Educational Publishers Inc
  • New Jersey, United States
  • English
  • 0321445619
  • 9780321445612
  • 522,963

Table of contents

Chapter 1: Rails Environment and Configurations 1
Startup 2 Default Environment Settings 2

Bootstrapping 3

RubyGems 5

Initializer 6

Default Load Paths 6

Rails, Modules, and Auto-Loading Code 7

Builtin Rails Info 8

Configuration 9

Additional Configuration 13

Development Mode 14

Automatic Class Reloading 15

The Rails Class Loader 15

Test Mode 17

Production Mode 17

Logging 18

Rails Log Files 20

Log File Analysis 22

Syslog 24

Conclusion 25

References 25



Chapter 2 Working with Controllers 27

The Dispatcher: Where It All Begins 28

Request Handling 28

Getting Intimate with the Dispatcher 29

Render unto View... 31

When in Doubt, Render 32

Explicit Rendering 33

Rendering Another Action's Template 33

Rendering a Different Template Altogether 34

Rendering a Partial Template 35

Rendering Inline Template Code 35

Rendering Text 35

Rendering Other Types of Structured Data 36

Rendering Nothing 36

Rendering Options 37

Redirecting 39

Controller/View Communication 42

Filters 43

Filter Inheritance 44

Filter Types 46

Filter Chain Ordering 47

Around Filters 48

Filter Chain Skipping 49

Filter Conditions 50

Filter Chain Halting 50

Streaming 51

send_data(data, options = {}) 51

send_file(path, options = {}) 52

Letting the Web Server Send Files 55

Conclusion 56

References 56



Chapter 3 Routing 57

The Two Purposes of Routing 58

Bound Parameters 60

Wildcard Components ("Receptors") 61

Static Strings 62

The routes.rb File 63

The Default Route 65

Spotlight on the :id Field 66

Default Route Generation 67

Modifying the Default Route 68

The Ante-Default Route and respond_to 69

respond_to and the HTTP-Accept Header 70

The Empty Route 71

Writing Custom Routes 72

Using Static Strings 72

Using Your Own "Receptors" 73

A Note on Route Order 75

Using Regular Expressions in Routes 76

Default Parameters and the url_for Method 76

What Happened to :id? 77

Using Literal URLs 79

Route Globbing 79

Globbing Key-Value Pairs 80

Named Routes 81

Creating a Named Route 81

The Question of Using name_path Versus name_url 82

Considerations 83

What to Name Your Routes 83

Argument Sugar 84

A Little More Sugar with Your Sugar? 85

The Special Scope Method with_options 86

Conclusion 88

References 88



Chapter 4 REST, Resources, and Rails 89

REST in a Rather Small Nutshell 89

REST in Rails 91

Routing and CRUD 92

Resources and Representations 93

REST Resources and Rails 93

From Named Routes to REST Support 94

Reenter the HTTP Verb 96

The Standard RESTful Controller Actions 96

The PUT and DELETE Cheat 98

Singular and Plural RESTful Routes 98

The Special Pairs: new/create and edit/update 99

Singular Resource Routes 100

Nested Resources 101

Setting :path_prefix Explicitly 103

Setting :name_prefix Explicitly 103

Specifying RESTful Controllers Explicitly 105

All Together Now 105

Considerations 107

Deep Nesting? 108

RESTful Route Customizations 110

Extra Member Routes 110

Extra Collection Routes 111

Considerations 112

Controller-Only Resources 113

Different Representations of Resources 115

The respond_to Method 116

Formatted Named Routes 117

The RESTful Rails Action Set 117

Index 118

Show 121

Destroy 121

New and Create 123

Edit and Update 124

Conclusion 125

Reference 126



Chapter 5 Reflecting on Rails Routing 127

Examining Routes in the Application Console 127

Dumping Routes 128

Anatomy of a Route Object 129

Recognition and Generation in the Console 132

Named Routes in the Console 134

Testing Routes 135

The Routing Navigator Plugin 136

Conclusion 137

References 137



Chapter 6 Working with ActiveRecord 139

The Basics 140

Migrations 142

Creating Migrations 143

Naming Migrations 144

Migration Pitfalls 145

Migration API 146

Defining Columns 149

Macro-Style Methods 155

Relationship Declarations 155

Convention over Configuration 156

Pluralization 157

Setting Names Manually 158

Legacy Naming Schemes 159

Defining Attributes 160

Default Attribute Values 160

Serialized Attributes 162

CRUD: Creating, Reading, Updating, Deleting 163

Creating New ActiveRecord Instances 163

Reading ActiveRecord Objects 164

Reading and Writing Attributes 166

Accessing and Manipulating Attributes Before They Are Typecast 168

Reloading 169

Dynamic Attribute-Based Finders 169

Custom SQL Queries 171

The Query Cache 172

Updating 174

Updating by Condition 176

Updating a Particular Instance 176

Updating Specific Attributes 177

Convenience Updaters 177

Controlling Access to Attributes 178

Deleting and Destroying 178

Database Locking 179

Optimistic Locking 180

Pessimistic Locking 182

Considerations 183

Advanced Finding 183

Conditions 183

Ordering of Find Results 186

Limit and Offset 186

Select Option 187

From Option 188

Group By Option 188

Locking Option 189

Joining and Including Associations 189

Read Only 189

Connections to Multiple Databases in Different Models 189

Using the Database Connection Directly 191

The DatabaseStatements Module 191

Other Connection Methods 194

Other Configuration Options 196

Conclusion 197

References 198





Chapter 7 ActiveRecord Associations 199

The Association Hierarchy 199

One-to-Many Relationships 201

Adding Associated Objects to a Collection 203

AssociationCollection Methods 204

The belongs_to Association 207

Reloading the Association 208

Building and Creating Related Objects via the Association 208

belongs_to Options 209

The has_many Association 215

has_many Options 216

Proxy Methods 224

Many-to-Many Relationships 225

has_and_belongs_to_many 225

has_many :through 233

has_many :through Options 238

:source 238

One-to-One Relationships 241

has_one 241

has_one Options 244

:Unsaved Objects and Associations 246

One-to-One Associations 246

Collections 247

Association Extensions 247

The AssociationProxy Class 249

reload and reset 249

proxy_owner, proxy_reflection, and proxy_target 249

Conclusion 250

References 251



Chapter 8 ActiveRecord Validations 253

Finding Errors 253

The Simple Declarative Validations 254

validates_acceptance_of 254

validates_associated 255

validates_confirmation_of 256

validates_each 256

validates_inclusion_of and validates_exclusion_of 257

validates_existence_of 257

validates_format_of 258

validates_length_of 259

validates_numericality_of 260

validates_presence_of 261

validates_uniqueness_of 261

RecordInvalid 263

Common Validation Options 263

:allow_nil 263

:if 263

:message 263

:on 264

Conditional Validation 264

Usage and Considerations 265

Working with the Errors Object 266

Manipulating the Errors Collection 267

Checking for Errors 267

Custom Validation 268

Skipping Validations 269

Conclusion 270

Reference 270



Chapter 9 Advanced ActiveRecord 271

Callbacks 271

Callback Registration 272

Matched before/after Callbacks 274

Halting Execution 274

Callback Usages 275

Special Callbacks: after_initialize and after_find 278

Callback Classes 279

Observers 282

Naming Conventions 283

Registration of Observers 283

Timing 284

Single-Table Inheritance (STI) 284

Mapping Inheritance to the Database 286

STI Considerations 288

STI and Associations 288

Abstract Base Model Classes 291

Polymorphic has_many Relationships 292

In the Case of Models with Comments 293

Considerations about has_many 296

Modules for Reusing Common Behavior 296

A Review of Class Scope and Contexts 299

The included Callback 300

Modifying ActiveRecord Classes at Runtime 301

Considerations 303

Ruby and Domain-Specific Languages 303

Conclusion 305

References 305



Chapter 10 ActionView 307

ERb Basics 308

ERb Practice 308

Tightening Up ERb Output 310

Commenting Out ERb Delimiters 310

Conditional Output 311

RHTML? RXML? RJS? 311

Layouts and Templates 312

Yielding Content 313

Template Variables 315

Protecting the Integrity of Your View from User-Submitted Content 319

Partials 320

Simple Use Cases 321

Reuse of Partials 322

Shared Partials 323

Passing Variables to Partials 324

Render Collections 325

Logging 327

Caching 327

Caching in Development Mode? 328

Page Caching 328

Action Caching 328

Fragment Caching 330

Expiration of Cached Content 333

Automatic Cache Expiry with Sweepers 335

Cache Logging 337

Action Cache Plugin 337

Cache Storage 338

Conclusion 340

References 340



Chapter 11 All About Helpers 341

ActiveRecordHelper 342

Reporting Validation Errors 342

Automatic Form Creation 344

Customizing the Way Validation Errors Are Highlighted 346

AssetTagHelper 348

Head Helpers 348

For Plugins Only, Add Default JavaScript Includes 352

BenchmarkHelper 353

benchmark(message = "Benchmarking", level = :info) 353

CacheHelper 353

CaptureHelper 354

capture(&block) 354

content_for(name, &block) 354

DateHelper 355

The Date and Time Selection Helpers 355

The Individual Date and Time Select Helpers 356

Common Options for Date Selection Helpers 359

distance_in_time Methods with Complex Descriptive Names 359

DebugHelper 361

FormHelper 362

Creating Forms for ActiveRecord Models 362

How Form Helpers Get Their Values 370

FormOptionsHelper 371

Select Helpers 371

Option Helpers 373

FormTagHelper 378

check_box_tag(name, value = "1", checked = false, options = {}) 378

end_form_tag 378

file_field_tag(name, options = {}) 378

form_tag(url_for_options = {}, options = {}, *parameters_for_url, &block) 379

hidden_field_tag(name, value = nil, options = {}) 380

image_submit_tag(source, options = {}) 380

password_field_tag(name = "password", value = nil, options = {}) 380

radio_button_tag(name, value, checked = false, options = {}) 380

select_tag(name, option_tags = nil, options = {}) 380

start_form_tag 380

submit_tag(value = "Save changes", options = {}) 381

text_area_tag(name, content = nil, options = {}) 381

text_field_tag(name, value = nil, options = {}) 381

JavaScriptHelper 381

button_to_function(name, function, html_options={}, &block) 381

define_javascript_functions() 382

escape_javascript(javascript) 382

javascript_tag(content, html_options={}) 382

link_to_function(name, function, html_options={}, &block) 382

NumberHelper 383

human_size(size, precision=1) 383

number_to_currency(number, options = {}) 383

number_to_human_size(size, precision=1) 384

number_to_percentage(number, options = {}) 384

number_to_phone(number, options = {}) 385

number_with_delimiter(number, delimiter=",", separator=".") 385

number_with_precision(number, precision=3) 385

PaginationHelper 386

will_paginate 386

paginator 387

Paginating Find 388

RecordIdentificationHelper 388

dom_class(record_or_class, prefix = nil) 389

dom_id(record, prefix = nil) 389

partial_path(record_or_class) 389

RecordTagHelper 390

content_tag_for(tag_name, record, *args, &block) 390

div_for(record, *args, &block) 391

TagHelper 391

cdata_section(content) 391

content_tag(name, content = nil, options = nil, &block) 391

escape_once(html) 392

tag(name, options = nil, open = false) 392

TextHelper 393

auto_link(text, link = :all, href_options = {}, &block) 393

concat(string, binding) 393

cycle(first_value, *values) 394

excerpt(text, phrase, radius = 100, excerpt_string = "...") 395

highlight(text, phrases, highlighter = `\1') 395

markdown(text) 396

pluralize(count, singular, plural = nil) 396

reset_cycle(name = "default") 397

sanitize(html) 397

simple_format(text) 398

strip_links(text) 398

strip_tags(html) 398

textilize(text) 399

textilize_without_paragraph(text) 399

truncate(text, length = 30, truncate_string = "...") 399

word_wrap(text, line_width = 80) 400

UrlHelper 400

button_to(name, options = {}, html_options = {}) 400

current_page?(options) 401

link_to(name, options = {}, html_options = nil) 401

link_to_if(condition, name, options = {}, html_options = {}, &block) 403

link_to_unless(condition, name, options = {}, html_options = {}, &block) 403

link_to_unless_current(name, options = {}, html_options = {}, &block) 403

mail_to(email_address, name = nil, html_options = {}) 404

url_for(options = {}) 405

Relying on Named Routes 406

Writing Your Own Helpers 407

Small Optimizations: The Title Helper 407

Encapsulating View Logic: The photo_for Helper 408

Smart View: The breadcrumbs Helper 409

Wrapping and Generalizing Partials 410

A tiles Helper 410

Generalizing Partials 414

Conclusion 417

References 417



Chapter 12 Ajax on Rails 419

Prototype 420

FireBug 421

The Prototype API 421

Top-Level Functions 422

Class 424

Extensions to JavaScript's Object Class 425

Extensions to JavaScript's Array Class 426

Extensions to the document Object 428

Extensions to the Event Class 428

Extensions to JavaScript's Function Class 430

Extensions to JavaScript's Number Class 432

Extensions to JavaScript's String class 433

string.dasherize() 434

string.escapeHTML() and string.unescapeHTML() 434

string.evalScripts() and string.extractScripts() 434

string.gsub(pattern, replacement) and string.sub(pattern, replacement, count) 434

string.scan(pattern, iterator) 434

string.strip() 435

string.stripScripts() and string.stripTags() 435

string.parseQuery() and string.toQueryParams() 435

string.toArray() 435

string.truncate(length, truncationString) 435

string.underscore() 436

The Ajax Object 436

Ajax.Responders 437

Enumerable 437

Hash 443

ObjectRange 444

The Prototype Object 445

The PrototypeHelper Module 445

link_to_remote 445

remote_form_for 449

periodically_call_remote 451

observe_field 451

observe_form 453

RJS-Writing Javascript in Ruby 453

RJS Templates 455
show more

Review Text

The expert guide to building Ruby on Rails applications

Ruby on Rails strips complexity from the development process, enabling professional developers to focus on what matters most: delivering business value. Now, for the first time, thereâ??s a comprehensive, authoritative guide to building production-quality software with Rails. Pioneering Rails developer Obie Fernandez and a team of experts illuminate the entire Rails API, along with the Ruby idioms, design approaches, libraries, and plug-ins that make Rails so valuable. Drawing on their unsurpassed experience, they address the real challenges development teams face, showing how to use Railsâ?? tools and best practices to maximize productivity and build polished applications users will enjoy.

Using detailed code examples, Obie systematically covers Railsâ?? key capabilities and subsystems. He presents advanced programming techniques, introduces open source libraries that facilitate easy Rails adoption, and offers important insights into testing and production deployment. Dive deep into the Rails codebase together, discovering why Rails behaves as it doesâ?? and how to make it behave the way you want it to.

This book will help you

Increase your productivity as a web developer

Realize the overall joy of programming with Ruby on Rails

Learn whatâ??s new in Rails 2.0

Drive design and protect long-term maintainability with TestUnit and RSpec

Understand and manage complex program flow in Rails controllers

Leverage Railsâ?? support for designing REST-compliant APIs

Master sophisticated Rails routing concepts and techniques

Examine and troubleshoot Rails routing

Make the most of ActiveRecord object-relational mapping

Utilize Ajax within your Rails applications

Incorporate logins and authentication into your application

Extend Rails with the best third-party plug-ins and write your own

Integrate email services into your applications with ActionMailer

Choose the right Rails production configurations

Streamline deployment with Capistrano
show more

About Obie Fernandez

Obie Fernandez is a recognized tech industry leader and independent consultant. He has been hacking computers since he got his first Commodore VIC-20 in the eighties, and found himself in the right place and time as a programmer on some of the first Java enterprise projects of the mid-nineties. He moved to Atlanta, Georgia, in 1998 and gained prominence as lead architect of local startup success MediaOcean. He also founded the Extreme Programming (later Agile Atlanta) User Group and was that group's president and organizer for several years. In 2004, he made the move back into the enterprise, tackling high-risk, progressive projects for world-renowned consultancy ThoughtWorks. He has been evangelizing Ruby and Rails online via blog posts and publications since early 2005, and earned himself quite a bit of notoriety (and trash talking) from his old friends in the Java open-source community. Since then, he has presented on a regular basis at numerous industry events and user group meetings, and even does the occasional training gig for corporations and groups wanting to get into Rails development. Nowadays, Obie specializes in the development and marketing of large-scale, web-based applications.
show more

Rating details

261 ratings
3.81 out of 5 stars
5 26% (68)
4 38% (100)
3 28% (72)
2 8% (20)
1 0% (1)
Book ratings by Goodreads
Goodreads is the world's largest site for readers with over 50 million reviews. We're featuring millions of their reader ratings on our book pages to help you find your new favourite book. Close X