O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The Rails 5 Way, Fourth Edition

Book Description

The “Bible” for Rails Development: Fully Updated for Rails 5

“When I read The Rails Way for the first time, I felt like I truly understood Rails for the first time.”

–Steve Klabnik, Rails contributor and mentor

The Rails™ 5 Way is the comprehensive, authoritative reference guide for professionals delivering production-quality code using modern Ruby on Rails. Obie Fernandez illuminates the entire Rails 5 API, its most powerful idioms, design approaches, and libraries. He presents new and updated content on Action Cable, RSpec 3.4, Turbolinks 5.0, the Attributes API, and many other enhancements, both major and subtle.

 

Through detailed code examples, you’ll dive deep into Ruby on Rails, discover why it’s designed as it is, and learn to make it do exactly what you want. Proven in thousands of production systems, the knowledge in this book will maximize your productivity and help you build more successful solutions.

  • Build powerful, scalable, REST-compliant back-end services
  • Program complex program flows using Action Controller
  • Represent models, relationships, and operations in Active Record, and apply advanced Active Record techniques
  • Smoothly evolve database schema via Migrations
  • Craft front-ends with ActionView and the Asset Pipeline
  • Optimize performance and scalability with caching and Turbolinks 5.0
  • Improve your productivity using Haml HTML templating
  • Secure your systems against attacks like SQL Injection, XSS, and XSRF
  • Integrate email using Action Mailer
  • Enable real-time, websockets-based browser behavior with Action Cable
  • Improve responsiveness with background processing
  • Build “API-only” back-end projects that speak JSON
  • Leverage enhancements to Active Job, serialization, and Ajax support

Register your product at informit.com/register for convenient access to downloads, updates, and/or corrections as they become available.

Table of Contents

  1. Cover Page
  2. Title Page
  3. Copyright Page
  4. Contents
  5. Foreword by Giles Bowkett
  6. Foreword to the Previous Edition by Steve Klabnik
  7. Foreword to the Previous Edition by David Heinemeier Hansson
  8. Foreword to the Previous Edition by Yehuda Katz
  9. Introduction
  10. Acknowledgments
  11. About the Author
  12. Chapter 1 Rails Configuration and Environments
    1. 1.1 Bundler
    2. 1.2 Startup Scripts
    3. 1.3 Default Initializers
    4. 1.4 Other Common Initializers
    5. 1.5 Spring Application Preloader
    6. 1.6 Development Mode
    7. 1.7 Test Mode
    8. 1.8 Production Mode
    9. 1.9 Configuring a Database
    10. 1.10 Configuring Application Secrets
    11. 1.11 Logging
    12. 1.12 Conclusion
  13. Chapter 2 Routing
    1. 2.1 The Two Purposes of Routing
    2. 2.2 The routes.rb File
    3. 2.3 Route Globbing
    4. 2.4 Named Routes
    5. 2.5 Scoping Routing Rules
    6. 2.6 Listing Routes
    7. 2.7 Conclusion
  14. Chapter 3 REST, Resources, and Rails
    1. 3.1 REST in a Rather Small Nutshell
    2. 3.2 Resources and Representations
    3. 3.3 REST in Rails
    4. 3.4 Routing and CRUD
    5. 3.5 The Standard RESTful Controller Actions
    6. 3.6 Singular Resource Routes
    7. 3.7 Nested Resources
    8. 3.8 Routing Concerns
    9. 3.9 RESTful Route Customizations
    10. 3.10 Controller-Only Resources
    11. 3.11 Different Representations of Resources
    12. 3.12 The RESTful Rails Action Set
    13. 3.13 Conclusion
  15. Chapter 4 Working with Controllers
    1. 4.1 Rack
    2. 4.2 Action Dispatch: Where It All Begins
    3. 4.3 Render unto View . . .
    4. 4.4 Additional Layout Options
    5. 4.5 Redirecting
    6. 4.6 Controller/View Communication
    7. 4.7 Action Callbacks
    8. 4.8 Streaming
    9. 4.9 Variants
    10. 4.10 Conclusion
  16. Chapter 5 Working with Active Record
    1. 5.1 The Basics
    2. 5.2 Macro-Style Methods
    3. 5.3 Defining Attributes
    4. 5.4 CRUD: Creating, Reading, Updating, Deleting
    5. 5.5 Database Locking
    6. 5.6 Querying
    7. 5.7 Ignoring Columns
    8. 5.8 Connections to Multiple Databases in Different Models
    9. 5.9 Using the Database Connection Directly
    10. 5.10 Custom SQL Queries
    11. 5.11 Other Configuration Options
    12. 5.12 Conclusion
  17. Chapter 6 Active Record Migrations
    1. 6.1 Creating Migrations
    2. 6.2 Defining Columns
    3. 6.3 Transactions
    4. 6.4 Data Migration
    5. 6.5 Database Schema
    6. 6.6 Database Seeding
    7. 6.7 Database-Related Tasks
    8. 6.8 Conclusion
  18. Chapter 7 Active Record Associations
    1. 7.1 The Association Hierarchy
    2. 7.2 One-to-Many Relationships
    3. 7.3 Belongs to Associations
    4. 7.4 Has Many Associations
    5. 7.5 Many-to-Many Relationships
    6. 7.6 One-to-One Relationships
    7. 7.7 Working with Unsaved Objects and Associations
    8. 7.8 Association Extensions
    9. 7.9 The CollectionProxy Class
    10. 7.10 Conclusion
  19. Chapter 8 Validations
    1. 8.1 Finding Errors
    2. 8.2 The Simple Declarative Validations
    3. 8.3 Common Validation Options
    4. 8.4 Conditional Validation
    5. 8.5 Short-Form Validation
    6. 8.6 Custom Validation Techniques
    7. 8.7 Skipping Validations
    8. 8.8 Working with the Errors Hash
    9. 8.9 Testing Validations with Shoulda
    10. 8.10 Conclusion
  20. Chapter 9 Advanced Active Record
    1. 9.1 Scopes
    2. 9.2 Callbacks
    3. 9.3 Attributes API
    4. 9.4 Serialized Attributes
    5. 9.5 Enums
    6. 9.6 Generating Secure Tokens
    7. 9.7 Calculation Methods
    8. 9.8 Batch Operations
    9. 9.9 Single-Table Inheritance (STI)
    10. 9.10 Abstract Base Model Classes
    11. 9.11 Polymorphic has_many Relationships
    12. 9.12 Foreign-Key Constraints
    13. 9.13 Modules for Reusing Common Behavior
    14. 9.14 Value Objects
    15. 9.15 Non-persisted Models
    16. 9.16 Modifying Active Record Classes at Runtime
    17. 9.17 PostgreSQL
    18. 9.18 Conclusion
  21. Chapter 10 Action View
    1. 10.1 Layouts and Templates
    2. 10.2 Partials
    3. 10.3 Conclusion
  22. Chapter 11 All about Helpers
    1. 11.1 ActiveModelHelper
    2. 11.2 AssetTagHelper
    3. 11.3 AssetUrlHelper
    4. 11.4 AtomFeedHelper
    5. 11.5 CacheHelper
    6. 11.6 CaptureHelper
    7. 11.7 ControllerHelper
    8. 11.8 CsrfHelper
    9. 11.9 DateHelper
    10. 11.10 DebugHelper
    11. 11.11 FormHelper
    12. 11.12 FormOptionsHelper
    13. 11.13 FormTagHelper
    14. 11.14 JavaScriptHelper
    15. 11.15 NumberHelper
    16. 11.16 OutputSafetyHelper
    17. 11.17 RecordTagHelper
    18. 11.18 RenderingHelper
    19. 11.19 SanitizeHelper
    20. 11.20 TagHelper
    21. 11.21 TextHelper
    22. 11.22 TranslationHelper and the I18n API
    23. 11.23 UrlHelper
    24. 11.24 Writing Your Own View Helpers
    25. 11.25 Wrapping and Generalizing Partials
    26. 11.26 Conclusion
  23. Chapter 12 Haml
    1. 12.1 Getting Started
    2. 12.2 The Basics
    3. 12.3 Doctype
    4. 12.4 Comments
    5. 12.5 Evaluating Ruby Code
    6. 12.6 Helpers
    7. 12.7 Filters
    8. 12.8 Haml and Content
    9. 12.9 Configuration Options
    10. 12.10 Conclusion
  24. Chapter 13 Session Management
    1. 13.1 What to Store in the Session
    2. 13.2 Storage Mechanisms
    3. 13.3 Cookies
    4. 13.4 Conclusion
  25. Chapter 14 Authentication and Authorization
    1. 14.1 Warden
    2. 14.2 Devise
    3. 14.3 has_secure_password
    4. 14.4 Pundit
    5. 14.5 Conclusion
  26. Chapter 15 Security
    1. 15.1 Password Management
    2. 15.2 Log Masking
    3. 15.3 SSL (Secure Sockets Layer)
    4. 15.4 Model Mass-Assignment Attributes Protection
    5. 15.5 SQL Injection
    6. 15.6 Cross-Site Scripting (XSS)
    7. 15.7 XSRF (Cross-Site Request Forgery)
    8. 15.8 Session Fixation Attacks
    9. 15.9 Keeping Secrets
    10. 15.10 Conclusion
  27. Chapter 16 Action Mailer
    1. 16.1 Mailer Models
    2. 16.2 Previews
    3. 16.3 Receiving Emails
    4. 16.4 Testing Email Content
    5. 16.5 Sending via API
    6. 16.6 Configuration
    7. 16.7 Conclusion
  28. Chapter 17 Caching and Performance
    1. 17.1 View Caching
    2. 17.2 Data Caching
    3. 17.3 Control of Web Caching
    4. 17.4 ETags
    5. 17.5 Conclusion
  29. Chapter 18 Background Processing
    1. 18.1 Active Job
    2. 18.2 Queueing Backends
    3. 18.3 Rails Runner
    4. 18.4 Conclusion
  30. Chapter 19 Asset Pipeline
    1. 19.1 Introduction to Asset Management
    2. 19.2 Organization. Where Does Everything Go?
    3. 19.3 Manifest Files
    4. 19.4 Custom Format Handlers
    5. 19.5 Post-Processing
    6. 19.6 Helpers
    7. 19.7 Fingerprinting
    8. 19.8 Serving the Files
    9. 19.9 Rake Tasks
    10. 19.10 Yarn
    11. 19.11 Webpack
    12. 19.12 Conclusion
  31. Chapter 20 Ajax on Rails
    1. 20.1 Unobtrusive JavaScript
    2. 20.2 Ajax and JSON
    3. 20.3 Ajax and HTML
    4. 20.4 JSONP Requests
    5. 20.5 Conclusion
  32. Chapter 21 Turbolinks
    1. 21.1 Turbolinks Usage
    2. 21.2 Building Your Turbolinks Application
    3. 21.3 Understanding Turbolinks Caching
    4. 21.4 Making Transformations Idempotent
    5. 21.5 Responding to Page Updates
    6. 21.6 Persisting Elements across Page Loads
    7. 21.7 Advanced Turbolinks
    8. 21.8 Turbolinks API Reference
    9. 21.9 Turbolinks Events
    10. 21.10 Conclusion
  33. Chapter 22 Action Cable
    1. 22.1 Web Sockets
    2. 22.2 Publish-Subscribe Pattern
    3. 22.3 Connections
    4. 22.4 Channels
    5. 22.5 Subscriptions
    6. 22.6 Streams
    7. 22.7 Subscriptions Revisited (Browser-Side)
    8. 22.8 Rebroadcasting
    9. 22.9 Channel Actions
    10. 22.10 Configuration
    11. 22.11 Running Stand-Alone Cable Servers
    12. 22.12 Generator
    13. 22.13 Conclusion
  34. Chapter 23 RSpec
    1. 23.1 Introduction
    2. 23.2 Behavior-Driven Development
    3. 23.3 Basic Syntax and API
    4. 23.4 Custom Expectation Matchers
    5. 23.5 Helper Methods
    6. 23.6 Shared Behaviors
    7. 23.7 Shared Context
    8. 23.8 Mocks and Stubs
    9. 23.9 Running Specs
    10. 23.10 Factory Girl
    11. 23.11 RSpec and Rails
    12. 23.12 Feature Specs with Capybara
    13. 23.13 Working with Files in Your Specs
    14. 23.14 RSpec Tools
    15. 23.15 Conclusion
  35. Appendix A Active Model API Reference
    1. A.1 AttributeAssignment
    2. A.2 AttributeMethods
    3. A.3 Callbacks
    4. A.4 Conversion
    5. A.5 Dirty
    6. A.6 Errors
    7. A.7 ForbiddenAttributesError
    8. A.8 Lint::Tests
    9. A.9 MissingAttributeError
    10. A.10 Model
    11. A.11 Name
    12. A.12 Naming
    13. A.13 SecurePassword
    14. A.14 Serialization
    15. A.15 Serializers::JSON
    16. A.16 Translation
    17. A.17 Type
    18. A.18 ValidationError
    19. A.19 Validations
    20. A.20 Validator
  36. Appendix B Active Support API Reference
    1. B.1 Array
    2. B.2 Autoload
    3. B.3 BacktraceCleaner
    4. B.4 Benchmark
    5. B.5 Benchmarkable
    6. B.6 BigDecimal
    7. B.7 Cache::FileStore
    8. B.8 Cache::MemCacheStore
    9. B.9 Cache::MemoryStore
    10. B.10 Cache::NullStore
    11. B.11 Cache::Store
    12. B.12 CachingKeyGenerator
    13. B.13 Callbacks
    14. B.14 Class
    15. B.15 Concern
    16. B.16 Configurable
    17. B.17 Date
    18. B.18 DateAndTime
    19. B.19 DateTime
    20. B.20 Dependencies
    21. B.21 DescendantsTracker
    22. B.22 Digest::UUID
    23. B.23 Duration
    24. B.24 Enumerable
    25. B.25 ERB::Util
    26. B.26 EventedFileUpdateChecker
    27. B.27 FalseClass
    28. B.28 File
    29. B.29 FileUpdateChecker
    30. B.30 Gzip
    31. B.31 Hash
    32. B.32 HashWithIndifferentAccess
    33. B.33 Inflector
    34. B.34 Inflector::Inflections
    35. B.35 Integer
    36. B.36 JSON
    37. B.37 Kernel
    38. B.38 KeyGenerator
    39. B.39 LazyLoadHooks
    40. B.40 Locale
    41. B.41 LogSubscriber
    42. B.42 Logger
    43. B.43 MessageEncryptor
    44. B.44 MessageVerifier
    45. B.45 Module
    46. B.46 Module::Concerning
    47. B.47 Multibyte::Chars
    48. B.48 Multibyte::Unicode
    49. B.49 NameError
    50. B.50 NilClass
    51. B.51 Notifications
    52. B.52 NumberHelper
    53. B.53 Numeric
    54. B.54 Object
    55. B.55 OrderedOptions
    56. B.56 ProxyObject
    57. B.57 Railtie
    58. B.58 Range
    59. B.59 Regexp
    60. B.60 Rescuable
    61. B.61 SecureRandom
    62. B.62 SecurityUtils
    63. B.63 String
    64. B.64 StringInquirer
    65. B.65 Subscriber
    66. B.66 TaggedLogging
    67. B.67 TestCase
    68. B.68 Testing::Assertions
    69. B.69 Thread
    70. B.70 Time
    71. B.71 TimeWithZone
    72. B.72 TimeZone
    73. B.73 TrueClass
    74. B.74 XmlMini
  37. Appendix C Rails API
    1. C.1 Rails API Mode
    2. C.2 JSON
  38. Index
  39. Credits
  40. Code Snippets