Synopses & Reviews
The “Bible” for Rails Development: Now Fully Updated for Rails 4.1
"When I read The Rails Way for the first time, I felt like I truly understood Rails for the first time.”
—From the Foreword by Steve Klabnik
Ruby on Rails 4 is leaner, tighter, and even more valuable to professional web developers. More than ever, it helps you focus on what matters most: delivering business value via clean and maintainable code.
The Rails™ 4 Way is the only comprehensive, authoritative guide to delivering production-quality code with Rails 4. Kevin Faustino joins pioneering Rails developer Obie Fernandez to illuminate the entire Rails 4 API, including its most powerful and modern idioms, design approaches, and libraries. They present extensive new and updated content on security, performance, caching, Haml, RSpec, Ajax, the Asset Pipeline, and more.
Through detailed code examples, you’ll dive deep into the Rails 4 code base, discover why Rails is designed as it is, and learn how to make it do exactly what you want. Proven in dozens of production systems, this book’s techniques will maximize your productivity and help you build more successful solutions. You’ll want to keep this guide by your computer—you’ll refer to it constantly.
This guide will help you
- Build powerful, scalable REST-compliant APIs
- Program complex program flows using Action Controller
- Represent models, relationships, CRUD operations, searches, validation, callbacks, and more
- Smoothly evolve application database schema via Migrations
- Apply advanced Active Record techniques: single-table inheritance, polymorphic models, and more
- Create visual elements with Action View and partials
- Optimize performance and scalability with view caching
- Master the highly productive Haml HTML templating engine
- Make the most of Rails’ approach to session management
- Secure your systems with Rails 4’s improved authentication and authorization
- Resist SQL Injection, XSS, XSRF, and other attacks
- Extend Rails with popular gems and plugins, and learn to write your own
- Integrate email services with Action Mailer
- Use Ajax via Rails 4 support for unobtrusive JavaScript
- Improve responsiveness with background processing
- Leverage Asset Pipeline to simplify development, improve perceived performance, and reduce server burdens
- Accelerate implementation and promote maintainability with RSpec
Review
Praise for The Rails Way
"For intermediates and above, I strongly recommend adding this title to your technical bookshelf. There is simply no other Rails title on the market at this time that offers the technical depth of the framework than The Rails™ 3 Way."
—Mike Riley, Dr. Dobb’s Journal
"I highly suggest you get this book. Software moves fast, especially the Rails API, but I feel this book has many core API and development concepts that will be useful for a while to come."
—Matt Polito, software engineer and member of Chicago Ruby User Group
"This book should live on your desktop if you’re a Rails developer. It’s nearly perfect in my opinion."
—Luca Pette, developer
"The Rails™ 3 Way is likely to take you from being a haphazard poke-a-stick-at-it programmer to a deliberate, skillful, productive, and confident RoR developer."
—Katrina Owen, JavaRanch
"I can positively say that it’s the single best Rails book ever published to date. By a long shot."
—Antonio Cangiano, software engineer and technical evangelist at IBM
"This book is a great crash course in Ruby on Rails! It doesn’t just document the features of Rails, it filters everything through the lens of an experienced Rails developer—so you come out a pro on the other side."
—Dirk Elmendorf, cofounder of Rackspace Inc. and Rails developer
"The key to The Rails Way is in the title. It literally covers the 'way' to do almost everything with Rails. Writing a truly exhaustive reference to the most popular web application framework used by thousands of developers is no mean feat. A thankful community of developers that has struggled to rely on scant documentation will embrace The Rails Way with open arms. A tour de force!"
—Peter Cooper, editor, Ruby Inside: The Ruby Blog
"In the past year, dozens of Rails books have been rushed to publication. A handful are good. Most regurgitate rudimentary information easily found on the Web. Only this book provides both the broad and deep technicalities of Rails. Nascent and expert developers, I recommend you follow The Rails Way."
—Martin Streicher, chief technology officer, McClatchy Interactive, former editor in chief of Linux Magazine
"Hal Fulton’s The Ruby Way has always been by my side as a reference while programming Ruby. Many times I had wished there was a book that had the same depth and attention to detail, only focused on the Rails framework. That book is now here and hasn’t left my desk for the past month."
—Nate Klaiber, Ruby programmer
"I knew soon after becoming involved with Rails that I had found something great. Now, with Obie’s book, I have been able to step into Ruby on Rails development coming from .NET and be productive right away. The applications I have created I believe to be a much better quality due to the techniques I learned using Obie’s knowledge."
—Robert Bazinet, InfoQ.com, .NET, and Ruby community editor and founding member of the Hartford Ruby Brigade
"Extremely well written; it’s a resource that every Rails programmer should have. Yes, it’s that good."
—Reuven Lerner, Linux Journal columnist
Synopsis
The "Bible" for Rails Development: Now Fully Updated for Rails 4.1
"When I read
The Rails Way for the first time, I felt like I truly understood Rails for the first time."
--From the Foreword by Steve Klabnik
Ruby on Rails 4 is leaner, tighter, and even more valuable to professional web developers. More than ever, it helps you focus on what matters most: delivering business value via clean and maintainable code.
The Rails(TM) 4 Way is the only comprehensive, authoritative guide to delivering production-quality code with Rails 4. Kevin Faustino joins pioneering Rails developer Obie Fernandez to illuminate the entire Rails 4 API, including its most powerful and modern idioms, design approaches, and libraries. They present extensive new and updated content on security, performance, caching, Haml, RSpec, Ajax, the Asset Pipeline, and more.
Through detailed code examples, you'll dive deep into the Rails 4 code base, discover why Rails is designed as it is, and learn how to make it do exactly what you want. Proven in dozens of production systems, this book's techniques will maximize your productivity and help you build more successful solutions. You'll want to keep this guide by your computer--you'll refer to it constantly.
This guide will help you
- Build powerful, scalable REST-compliant APIs
- Program complex program flows using Action Controller
- Represent models, relationships, CRUD operations, searches, validation, callbacks, and more
- Smoothly evolve application database schema via Migrations
- Apply advanced Active Record techniques: single-table inheritance, polymorphic models, and more
- Create visual elements with Action View and partials
- Optimize performance and scalability with view caching
- Master the highly productive Haml HTML templating engine
- Make the most of Rails' approach to session management
- Secure your systems with Rails 4's improved authentication and authorization
- Resist SQL Injection, XSS, XSRF, and other attacks
- Extend Rails with popular gems and plugins, and learn to write your own
- Integrate email services with Action Mailer
- Use Ajax via Rails 4 support for unobtrusive JavaScript
- Improve responsiveness with background processing
- Leverage Asset Pipeline to simplify development, improve perceived performance, and reduce server burdens
- Accelerate implementation and promote maintainability with RSpec
Synopsis
Enabling professional developers to focus on what matters most - deliver business value via clean and maintainable code - Ruby on Rails strips complexity from the development process. Rails was created in 2004 to maximize the use of the Ruby programming language in an open source web application framework, causing an explosion of interest in Ruby. In less than ten years, Ruby on Rails has forcefully taken its place as the dominant king of web frameworks, with the support of thousands of open source contributors.
The Rails™ 4 Way is the only comprehensive, authoritative guide to delivering production-quality code with Rails 4. Pioneering Rails expert Obie Fernandez and his team of leading Rails experts illuminate the entire set of Rails APIs, along with the idioms, design approaches, and libraries that make developing applications with Rails so powerful. Drawing on their unsurpassed experience and track record, they address the real challenges development teams face, showing how to use Rails to maximize your productivity.
Using numerous detailed code examples, the author systematically covers Rails key capabilities and subsystems, making this book a reference that you can depend on every day. He presents advanced Rails programming techniques that have been proven effective in day-to-day usage on dozens of production Rails systems and offers important insights into behavior-driven development and production considerations such as scalability. Dive deep into the subtleties of the asset pipeline and other advanced Rails topics such as security and scalability. The Rails 4 Way is your best guide for making Rails do exactly what you want it to do.
About the Author
Obie Fernandez 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. In 1998, he moved to Atlanta, Georgia, where is still resides, 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.
As founder of Hashrocket, one of the world’s best web design and development consultancies, Obie specialized in orchestrating the creation of large-scale, web-based applications, both for startups and mission-critical enterprise projects. In 2010, Obie sold his stake in Hashrocket and has been working with technology startups ever since.
Obie's evangelization of Ruby on Rails online, via blog posts and publications, dates to early 2005, and it has earned him quite a bit of notoriety (and trash talking) from his old friends in the Java open-source community. Since then, he has traveled around the world relentlessly promoting Rails at large industry conferences. The previous two editions of this book are considered the "bibles" of Ruby on Rails development and are bestsellers.
Obie still gets his hands dirty with code on a daily basis and posts regularly on various topics to his popular weblog at
Table of Contents
Foreword iii
Acknowledgments ix
About the Authors xi
Introduction xiii
About This Book xiii
Recommended Reading and Resources xiv
Goals xv
Prerequisites xvi
Required Technology xvi
1 Rails Environments and Configuration 1
1.1 Bundler 1
1.2 Startup and Application Settings 8<="" p="">
1.3 Development Mode 16
1.4 Test Mode 19
1.5 Production Mode 21
1.6 Logging 24
2 Routing 31
2.1 The Two Purposes of Routing 31
2.2 The routes.rb File 32
2.3 Route Globbing 44
2.4 Named Routes 45
2.5 Scoping Routing Rules 50
2.6 Listing Routes 52
2.7 Conclusion 53
3 REST, Resources, and Rails 55
3.1 REST in a Rather Small Nutshell 55
3.2 Resources and Representations 56
3.3 REST in Rails 56
3.4 Routing and CRUD 57
3.5 The Standard RESTful Controller Actions 60
3.6 Singular Resource Routes 63
3.7 Nested Resources 64
3.8 Routing Concerns 68
3.9 RESTful Route Customizations 69
3.10 Controller-Only Resources 73
3.11 Different Representations of Resources 75
3.12 The RESTful Rails Action Set 77
3.13 Conclusion 82
4 Working with Controllers 83
4.1 Rack 83
4.2 Action Dispatch: Where It All Begins 86
4.3 Render unto View… 89
4.4 Additional Layout Options 97
4.5 Redirecting 98
4.6 Controller/View Communication 101
4.7 Action Callbacks 102
4.8 Streaming 107
4.9 Conclusion 112
5 Working with Active Record 113
5.1 The Basics 113
5.2 Macro-Style Methods 114
5.3 Defining Attributes 117<="" p="">
5.4 CRUD: Creating, Reading, Updating, Deleting 121
5.5 Database Locking 134
5.6 Where Clauses 137
5.7 Connections to Multiple Databases in Different Models 148
5.8 Using the Database Connection Directly 149
5.9 Other Configuration Options 153
5.10 Conclusion 153
6 Active Record Migrations 155
6.1 Creating Migrations 155
6.2 Data Migration 168
6.3 schema.rb 170
6.4 Database Seeding 170
6.5 Database-Related Rake Tasks 171
6.6 Conclusion 175
7 Active Record Associations 177
7.1 The Association Hierarchy 177
7.2 One-to-Many Relationships 178
7.3 The belongs_to Association 186
7.4 The has_many Association 194
7.5 Many-to-Many Relationships 202
7.6 One-to-One Relationships 212
7.7 Working with Unsaved Objects and Associations 216
7.8 Association Extensions 217
7.9 The CollectionProxy Class 219
7.10 Conclusion 220
8 Validations 221
8.1 Finding Errors 221
8.2 The Simple Declarative Validations 221
8.3 Common Validation Options 231
8.4 Conditional Validation 233
8.5 Short-form Validation 235
8.6 Custom Validation Techniques 236
8.7 Skipping Validations 239
8.8 Working with the Errors Hash 239
8.9 Testing Validations with Shoulda 240
8.10 Conclusion 241
9 Advanced Active Record 243
9.1 Scopes 243
9.2 Callbacks 248
9.3 Calculation Methods 257
9.4 Single-Table Inheritance (STI) 259
9.5 Abstract Base Model Classes 265
9.6 Polymorphic has_many Relationships 266
9.7 Foreign-key Constraints 270
9.8 Modules for Reusing Common Behavior 270
9.9 Modifying Active Record Classes at Runtime 274
9.10 Using Value Objects 277
9.11 Non-Persisted Models 280
9.12 PostgreSQL enhancements 281
9.13 Conclusion 288
10 Action View 289
10.1 Layouts and Templates 289
10.2 Partials 298
10.3 Conclusion 304
11 All About Helpers 305
11.1 ActiveModelHelper 305
11.2 AssetTagHelper 311
11.3 AtomFeedHelper 318
11.4 CacheHelper 321
11.5 CaptureHelper 321
11.6 CsrfHelper 322
11.7 DateHelper 322
11.8 DebugHelper 329
11.9 FormHelper 329
11.10 FormOptionsHelper 343
11.11 FormTagHelper 350
11.12 JavaScriptHelper 355
11.13 NumberHelper 356
11.14 OutputSafetyHelper 361
11.15 RecordTagHelper 361
11.16 RenderingHelper 362
11.17 SanitizeHelper 362
11.18 TagHelper 364
11.19 TextHelper 366
11.20 TranslationHelper and the I18n API 370
11.21 UrlHelper 388
11.22 Writing Your Own View Helpers 392
11.23 Wrapping and Generalizing Partials 395
11.24 Conclusion 400
12 Haml 403
12.1 Getting Started 403<="" p="">
12.2 The Basics 404
12.3 Doctype 409
12.4 Comments 409
12.5 Evaluating Ruby Code 410
12.6 Helpers 412
12.7 Filters 413
12.8 Haml and Content 414
12.9 Configuration Options 415
12.10 Conclusion 417
13 Session Management 419
13.1 What to Store in the Session 419
13.2 Session Options 420
13.3 Storage Mechanisms 420
13.4 Cookies 424
13.5 Conclusion 426
14 Authentication and Authorization 427
14.1 Devise 427
14.2 has_secure_password 433
14.3 Pundit 438
14.4 Conclusion 444
15 Security 445
15.1 Password Management 445
15.2 Log Masking 447
15.3 SSL (Secure Sockets Layer) 447
15.4 Model mass-assignment attributes protection 448<="" p="">
15.5 SQL Injection 450
15.6 Cross-Site Scripting (XSS) 451
15.7 XSRF (Cross-Site Request Forgery) 454
15.8 Session Fixation Attacks 456
15.9 Keeping Secrets 457
15.10 Conclusion 458
16 Action Mailer 459
16.1 Setup 459
16.2 Mailer Models 459
16.3 Receiving Emails 466
16.4 Server Configuration 467
16.5 Testing Email Content 468
16.6 Conclusion 469
17 Caxhing and Performance 471
17.1 View Caching 471
17.2 Data Caching 486
17.3 Control of Web Caching 488
17.4 ETags 489
17.5 Conclusion 490
18 Ba : ground Processing 493
18.1 Delayed Job 493
18.2 Sidekiq 496
18.3 Resque 502
18.4 Rails Runner 507
18.5 Conclusion 509
19 Ajax on Rails 511
19.1 Unobtrusive JavaScript 512
19.2 Turbolinks 516
19.3 Ajax and JSON 518
19.4 Ajax and HTML 520
19.5 Ajax and JavaScript 522
19.6 Conclusion 523
20 Asset Pipeline 525
20.1 Asset Pipeline 525
20.2 Wish List 526
20.3 The Big Picture 526
20.4 Organization. Where does everything go? 526
20.5 Manifest files 527
20.6 Custom format handlers 532
20.7 Post-Processing 533
20.8 Helpers 534
20.9 Fingerprinting 536
20.10 Serving the files 536
20.11 Rake Tasks 538
20.12 Conclusion 539
21 RSpec 541
21.1 Introduction 541
21.2 Basic Syntax and API 544
21.3 Matchers 554
21.4 Custom Expectation Matchers 555
21.5 Shared Behaviors 558
21.6 Shared Context 558
21.7 RSpec’s Mocks and Stubs 559
21.8 Running Specs 561
21.9 RSpec Rails Gem 562
21.10 RSpec Tools 576
21.11 Conclusion 577
22 XML 579
22.1 The to_xml Method 579
22.2 The XML Builder 589
22.3 Parsing XML 591
22.4 Conclusion 592
Active Model API Reference 593
AttributeMethods 593
Callbacks 595
Conversion 597
Dirty 597
Errors 599
ForbiddenAttributesError 603
Lint::Tests 603
Model 603
Name 605
Naming 606
SecurePassword 606
Serialization 607
Serializers::JSON 607
Serializers::Xml 608
Translation 609
Validations 610
Validator 618
Active Support API Reference 619
Array 619
ActiveSupport::BacktraceCleaner 625
Benchmark 626
ActiveSupport::Benchmarkable 626
BigDecimal 627
ActiveSupport::Cache::Store 628
ActiveSupport::CachingKeyGenerator 632
ActiveSupport::Callbacks 633
Class 636
ActiveSupport::Concern 639
ActiveSupport::Concurrency 640
ActiveSupport::Configurable 640
Date 641
DateTime 650
ActiveSupport::Dependencies 655
ActiveSupport::Deprecation 661
ActiveSupport::DescendantsTracker 662
ActiveSupport::Duration 662
Enumerable 664
ERB::Util 665
FalseClass 666
File 666
Hash 667
ActiveSupport::Gzip 673
ActiveSupport::HashWithIndifferentAccess 673
ActiveSupport::Inflector::Inflections 674
Integer 680
ActiveSupport::JSON 681
Kernel 682
ActiveSupport::KeyGenerator 684
ActiveSupport::Logger 684
ActiveSupport::MessageEncryptor 685
ActiveSupport::MessageVerifier 685
Module 686
ActiveSupport::Multibyte::Chars 694
NilClass 699
ActiveSupport::Notifications 699
Object 709
ActiveSupport::OrderedHash 714
ActiveSupport::OrderedOptions 714
ActiveSupport::PerThreadRegistry 714
ActiveSupport::ProxyObject 715
ActiveSupport::Railtie 716
Range 717
Regexp 718
ActiveSupport::Rescuable 719
String 719
ActiveSupport::StringInquirer 729
Struct 729
ActiveSupport::Subscriber 729
Symbol 730
ActiveSupport::TaggedLogging 730
ActiveSupport::TestCase 730
ActiveSupport::Testing::Assertions 732
Thread 733
Time 734
ActiveSupport::TimeWithZone 744
ActiveSupport::TimeZone 745
TrueClass 749
ActiveSupport::XmlMini 749
Rails Essentials 753
Environmental Concerns 753
Essential Gems 754
Ruby Toolbox 761
Screencasts 761 55