SKIP THE SHIPPING
Use code NOSHIP during checkout to save 40% on eligible eBooks, now through January 5. Shop now.
Register your product to gain access to bonus material or receive a coupon.
This eBook includes the following formats, accessible from your Account page after purchase:
EPUB The open industry format known for its reflowable content and usability on supported mobile devices.
PDF The popular standard, used most often with the free Acrobat® Reader® software.
This eBook requires no passwords or activation to read. We customize your eBook by discreetly watermarking it with your name, making it uniquely yours.
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
Foreword by Steve Klabnik xxxix
Foreword to the Previous Edition by David Heinemeier Hansson xli
Foreword to the Previous Edition by Yehuda Katz xliii
Introduction xlv
Acknowledgments li
About the Authors liii
Chapter 1: Rails Environments and Configuration 1
1.1 Bundler 2
1.2 Startup and Application Settings 9
1.3 Development Mode 18
1.4 Test Mode 22
1.5 Production Mode 23
1.6 Configuring a Database 26
1.7 Configuring Application Secrets 27
1.8 Logging 29
1.9 Conclusion 35
Chapter 2: Routing 37
2.1 The Two Purposes of Routing 38
2.2 The routes.rb File 39
2.3 Route Globbing 51
2.4 Named Routes 53
2.5 Scoping Routing Rules 57
2.6 Listing Routes 60
2.7 Conclusion 61
Chapter 3: REST, Resources, and Rails 63
3.1 REST in a Rather Small Nutshell 63
3.2 Resources and Representations 64
3.3 REST in Rails 65
3.4 Routing and CRUD 66
3.5 The Standard RESTful Controller Actions 69
3.6 Singular Resource Routes 73
3.7 Nested Resources 74
3.8 Routing Concerns 78
3.9 RESTful Route Customizations 79
3.10 Controller-Only Resources 83
3.11 Different Representations of Resources 86
3.12 The RESTful Rails Action Set 88
3.13 Conclusion 92
Chapter 4: Working with Controllers 95
4.1 Rack 96
4.2 Action Dispatch: Where It All Begins 99
4.3 Render unto View… 102
4.4 Additional Layout Options 111
4.5 Redirecting 111
4.6 Controller/View Communication 115
4.7 Action Callbacks 116
4.8 Streaming 121
4.9 Variants 126
4.10 Conclusion 127
Chapter 5: Working with Active Record 129
5.1 The Basics 130
5.2 Macro-Style Methods 131
5.3 Defining Attributes 133
5.4 CRUD: Create, Read, Update, and Delete 138
5.5 Database Locking 151
5.6 Where Clauses 155
5.7 Connections to Multiple Databases in Different Models 165
5.8 Using the Database Connection Directly 167
5.9 Other Configuration Options 171
5.10 Conclusion 171
Chapter 6: Active Record Migrations 173
6.1 Creating Migrations 173
6.2 Data Migration 187
6.3 schema.rb 189
6.4 Database Seeding 190
6.5 Database-Related Rake Tasks 191
6.6 Conclusion 194
Chapter 7: Active Record Associations 195
7.1 The Association Hierarchy 195
7.2 One-to-Many Relationships 196
7.3 The belongs_to Association 205
7.4 The has_many Association 214
7.5 Many-to-Many Relationships 222
7.6 One-to-One Relationships 233
7.7 Working with Unsaved Objects and Associations 236
7.8 Association Extensions 238
7.9 The CollectionProxy Class 239
7.10 Conclusion 240
Chapter 8: Validations 241
8.1 Finding Errors 241
8.2 The Simple Declarative Validations 242
8.3 Common Validation Options 253
8.4 Conditional Validation 255
8.5 Short-Form Validation 256
8.6 Custom Validation Techniques 258
8.7 Skipping Validations 260
8.8 Working with the Errors Hash 261
8.9 Testing Validations with Shoulda 262
8.10 Conclusion 262
Chapter 9: Advanced Active Record 263
9.1 Scopes 263
9.2 Callbacks 268
9.3 Calculation Methods 278
9.4 Single-Table Inheritance (STI) 280
9.5 Abstract Base Model Classes 286
9.6 Polymorphic has_many Relationships 287
9.7 Enums 290
9.8 Foreign-Key Constraints 292
9.9 Modules for Reusing Common Behavior 292
9.10 Modifying Active Record Classes at Runtime 297
9.11 Using Value Objects 299
9.12 Nonpersisted Models 302
9.13 PostgreSQL Enhancements 304
9.14 Conclusion 311
Chapter 10: Action View 313
10.1 Layouts and Templates 314
10.2 Partials 322
10.3 Conclusion 329
Chapter 11: All about Helpers 331
11.1 ActiveModelHelper 331
11.2 AssetTagHelper 338
11.3 AtomFeedHelper 346
11.4 CacheHelper 348
11.5 CaptureHelper 348
11.6 CsrfHelper 349
11.7 DateHelper 349
11.8 DebugHelper 356
11.9 FormHelper 357
11.10 FormOptionsHelper 371
11.11 FormTagHelper 379
11.12 JavaScriptHelper 385
11.13 NumberHelper 385
11.14 OutputSafetyHelper 390
11.15 RecordTagHelper 390
11.16 RenderingHelper 391
11.17 SanitizeHelper 391
11.18 TagHelper 393
11.19 TextHelper 395
11.20 TranslationHelper and the I18n API 399
11.21 UrlHelper 418
11.22 Writing Your Own View Helpers 422
11.23 Wrapping and Generalizing Partials 425
11.24 Conclusion 431
Chapter 12: Haml 433
12.1 Getting Started 434
12.2 The Basics 434
12.3 Doctype 440
12.4 Comments 440
12.5 Evaluating Ruby Code 441
12.6 Helpers 443
12.7 Filters 444
12.8 Haml and Content 445
12.9 Configuration Options 446
12.10 Conclusion 448
Chapter 13: Session Management 449
13.1 What to Store in the Session 450
13.2 Session Options 451
13.3 Storage Mechanisms 451
13.4 Cookies 455
13.5 Conclusion 457
Chapter 14: Authentication and Authorization 459
14.1 Devise 459
14.2 has_secure_password 466
14.3 Pundit 470
14.4 Conclusion 476
Chapter 15: Security 477
15.1 Password Management 477
15.2 Log Masking 479
15.3 SSL (Secure Sockets Layer) 480
15.4 Model Mass-Assignment Attributes Protection 481
15.5 SQL Injection 483
15.6 Cross-Site Scripting (XSS) 484
15.7 XSRF (Cross-Site Request Forgery) 487
15.8 Session Fixation Attacks 490
15.9 Keeping Secrets 491
15.10 Conclusion 492
Chapter 16: Action Mailer 493
16.1 Setup 493
16.2 Mailer Models 494
16.3 Receiving Emails 500
16.4 Server Configuration 502
16.5 Testing Email Content 502
16.6 Previews 503
16.7 Conclusion 504
Chapter 17: Caching and Performance 505
17.1 View Caching 505
17.2 Data Caching 521
17.3 Control of Web Caching 523
17.4 ETags 524
17.5 Conclusion 526
Chapter 18: Background Processing 527
18.1 Delayed Job 528
18.2 Sidekiq 531
18.3 Resque 537
18.4 Rails Runner 541
18.5 Conclusion 543
Chapter 19: Ajax on Rails 545
19.1 Unobtrusive JavaScript 547
19.2 Turbolinks 551
19.3 Ajax and JSON 553
19.4 Ajax and HTML 555
19.5 Ajax and JavaScript 557
19.6 Conclusion 558
Chapter 20: Asset Pipeline 559
20.1 Asset Pipeline 560
20.2 Wish List 560
20.3 The Big Picture 561
20.4 Organization: Where Does Everything Go? 561
20.5 Manifest Files 561
20.6 Custom Format Handlers 567
20.7 Postprocessing 568
20.8 Helpers 569
20.9 Fingerprinting 571
20.10 Serving the Files 572
20.11 Rake Tasks 573
20.12 Conclusion 574
Chapter 21: RSpec 575
21.1 Introduction 575
21.2 Basic Syntax and API 578
21.3 Matchers 587
21.4 Custom Expectation Matchers 588
21.5 Shared Behaviors 591
21.6 Shared Context 592
21.7 RSpec’s Mocks and Stubs 592
21.8 Running Specs 595
21.9 RSpec Rails Gem 596
21.10 RSpec Tools 609
21.11 Conclusion 610
Chapter 22: XML 611
22.1 The to_xml Method 611
22.2 The XML Builder 620
22.3 Parsing XML 622
22.4 Conclusion 624
Appendix A: Active Model API Reference 625
A.1 AttributeMethods 625
A.2 Callbacks 627
A.3 Conversion 629
A.4 Dirty 629
A.5 Errors 631
A.6 ForbiddenAttributesError 635
A.7 Lint::Tests 635
A.8 Model 635
A.9 Name 636
A.10 Naming 638
A.11 SecurePassword 638
A.12 Serialization 638
A.13 Serializers::JSON 639
A.14 Serializers::Xml 639
A.15 Translation 640
A.16 Validations 641
A.17 Validator 648
Appendix B: Active Support API Reference 651
B.1 Array 651
B.2 ActiveSupport::BacktraceCleaner 657
B.3 Benchmark 658
B.4 ActiveSupport::Benchmarkable 658
B.5 BigDecimal 659
B.6 ActiveSupport::Cache::Store 660
B.7 ActiveSupport::CachingKeyGenerator 665
B.8 ActiveSupport::Callbacks 665
B.9 Class 668
B.10 ActiveSupport::Concern 671
B.11 ActiveSupport::Concurrency 672
B.12 ActiveSupport::Configurable 673
B.13 Date 673
B.14 DateTime 682
B.15 ActiveSupport::Dependencies 687
B.16 ActiveSupport::Deprecation 693
B.17 ActiveSupport::DescendantsTracker 694
B.18 ActiveSupport::Duration 695
B.19 Enumerable 696
B.20 ERB::Util 697
B.21 FalseClass 698
B.22 File 698
B.23 Hash 699
B.24 ActiveSupport::Gzip 704
B.25 ActiveSupport::HashWithIndifferentAccess 705
B.26 ActiveSupport::Inflector::Inflections 705
B.27 Integer 711
B.28 ActiveSupport::JSON 712
B.29 Kernel 712
B.30 ActiveSupport::KeyGenerator 714
B.31 ActiveSupport::Logger 714
B.32 ActiveSupport::MessageEncryptor 715
B.33 ActiveSupport::MessageVerifier 715
B.34 Module 716
B.35 ActiveSupport::Multibyte::Chars 724
B.36 NilClass 729
B.37 ActiveSupport::Notifications 729
B.38 Object 738
B.39 ActiveSupport::OrderedHash 743
B.40 ActiveSupport::OrderedOptions 743
B.41 ActiveSupport::PerThreadRegistry 744
B.42 ActiveSupport::ProxyObject 744
B.43 ActiveSupport::Railtie 745
B.44 Range 746
B.45 Regexp 747
B.46 ActiveSupport::Rescuable 748
B.47 String 748
B.48 ActiveSupport::StringInquirer 758
B.49 Struct 758
B.50 ActiveSupport::Subscriber 758
B.51 Symbol 759
B.52 ActiveSupport::TaggedLogging 759
B.53 ActiveSupport::TestCase 759
B.54 ActiveSupport::Testing::Assertions 761
B.55 Thread 762
B.56 Time 763
B.57 ActiveSupport::TimeWithZone 773
B.58 ActiveSupport::TimeZone 774
B.59 TrueClass 778
B.60 ActiveSupport::XmlMini 778
Appendix C: Rails Essentials 781
C.1 Environmental Concerns 781
C.2 Essential Gems 782
C.3 Ruby Toolbox 789
C.4 Screencasts 789
Index 791