You are previewing The Rails™ 4 Way, Third Edition.
O'Reilly logo
The Rails™ 4 Way, Third Edition

Book Description

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 Rails4 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

  • Table of Contents

    1. About This eBook
    2. Title Page
    3. Copyright Page
    4. Praise for The Rails Way
    5. Dedication Page
    6. Contents
    7. Foreword
    8. Foreword to the Previous Edition
    9. Foreword to the Previous Edition
    10. Introduction
      1. About This Book
      2. Recommended Reading and Resources
      3. Goals
      4. Prerequisites
      5. Required Technology
    11. Acknowledgments
    12. About the Authors
      1. Obie Fernandez
      2. Kevin Faustino
    13. Chapter 1. Rails Environments and Configuration
      1. 1.1 Bundler
      2. 1.2 Startup and Application Settings
      3. 1.3 Development Mode
      4. 1.4 Test Mode
      5. 1.5 Production Mode
      6. 1.6 Configuring a Database
      7. 1.7 Configuring Application Secrets
      8. 1.8 Logging
      9. 1.9 Conclusion
    14. 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
    15. 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
    16. 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
    17. 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: Create, Read, Update, and Delete
      5. 5.5 Database Locking
      6. 5.6 Where Clauses
      7. 5.7 Connections to Multiple Databases in Different Models
      8. 5.8 Using the Database Connection Directly
      9. 5.9 Other Configuration Options
      10. 5.10 Conclusion
    18. Chapter 6. Active Record Migrations
      1. 6.1 Creating Migrations
      2. 6.2 Data Migration
      3. 6.3 schema.rb
      4. 6.4 Database Seeding
      5. 6.5 Database-Related Rake Tasks
      6. 6.6 Conclusion
    19. Chapter 7. Active Record Associations
      1. 7.1 The Association Hierarchy
      2. 7.2 One-to-Many Relationships
      3. 7.3 The belongs_to Association
      4. 7.4 The has_many Association
      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
    20. 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
    21. Chapter 9. Advanced Active Record
      1. 9.1 Scopes
      2. 9.2 Callbacks
      3. 9.3 Calculation Methods
      4. 9.4 Single-Table Inheritance (STI)
      5. 9.5 Abstract Base Model Classes
      6. 9.6 Polymorphic has_many Relationships
      7. 9.7 Enums
      8. 9.8 Foreign-Key Constraints
      9. 9.9 Modules for Reusing Common Behavior
      10. 9.10 Modifying Active Record Classes at Runtime
      11. 9.11 Using Value Objects
      12. 9.12 Nonpersisted Models
      13. 9.13 PostgreSQL Enhancements
      14. 9.14 Conclusion
    22. Chapter 10. Action View
      1. 10.1 Layouts and Templates
      2. 10.2 Partials
      3. 10.3 Conclusion
    23. Chapter 11. All about Helpers
      1. 11.1 ActiveModelHelper
      2. 11.2 AssetTagHelper
      3. 11.3 AtomFeedHelper
      4. 11.4 CacheHelper
      5. 11.5 CaptureHelper
      6. 11.6 CsrfHelper
      7. 11.7 DateHelper
      8. 11.8 DebugHelper
      9. 11.9 FormHelper
      10. 11.10 FormOptionsHelper
      11. 11.11 FormTagHelper
      12. 11.12 JavaScriptHelper
      13. 11.13 NumberHelper
      14. 11.14 OutputSafetyHelper
      15. 11.15 RecordTagHelper
      16. 11.16 RenderingHelper
      17. 11.17 SanitizeHelper
      18. 11.18 TagHelper
      19. 11.19 TextHelper
      20. 11.20 TranslationHelper and the I18n API
      21. 11.21 UrlHelper
      22. 11.22 Writing Your Own View Helpers
      23. 11.23 Wrapping and Generalizing Partials
      24. 11.24 Conclusion
    24. 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
    25. Chapter 13. Session Management
      1. 13.1 What to Store in the Session
      2. 13.2 Session Options
      3. 13.3 Storage Mechanisms
      4. 13.4 Cookies
      5. 13.5 Conclusion
    26. Chapter 14. Authentication and Authorization
      1. 14.1 Devise
      2. 14.2 has_secure_password
      3. 14.3 Pundit
      4. 14.4 Conclusion
    27. 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
    28. Chapter 16. Action Mailer
      1. 16.1 Setup
      2. 16.2 Mailer Models
      3. 16.3 Receiving Emails
      4. 16.4 Server Configuration
      5. 16.5 Testing Email Content
      6. 16.6 Previews
      7. 16.7 Conclusion
    29. 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
    30. Chapter 18. Background Processing
      1. 18.1 Delayed Job
      2. 18.2 Sidekiq
      3. 18.3 Resque
      4. 18.4 Rails Runner
      5. 18.5 Conclusion
    31. Chapter 19. Ajax on Rails
      1. 19.1 Unobtrusive JavaScript
      2. 19.2 Turbolinks
      3. 19.3 Ajax and JSON
      4. 19.4 Ajax and HTML
      5. 19.5 Ajax and JavaScript
      6. 19.6 Conclusion
    32. Chapter 20. Asset Pipeline
      1. 20.1 Asset Pipeline
      2. 20.2 Wish List
      3. 20.3 The Big Picture
      4. 20.4 Organization: Where Does Everything Go?
      5. 20.5 Manifest Files
      6. 20.6 Custom Format Handlers
      7. 20.7 Postprocessing
      8. 20.8 Helpers
      9. 20.9 Fingerprinting
      10. 20.10 Serving the Files
      11. 20.11 Rake Tasks
      12. 20.12 Conclusion
    33. Chapter 21. RSpec
      1. 21.1 Introduction
      2. 21.2 Basic Syntax and API
      3. 21.3 Matchers
      4. 21.4 Custom Expectation Matchers
      5. 21.5 Shared Behaviors
      6. 21.6 Shared Context
      7. 21.7 RSpec’s Mocks and Stubs
      8. 21.8 Running Specs
      9. 21.9 RSpec Rails Gem
      10. 21.10 RSpec Tools
      11. 21.11 Conclusion
    34. Chapter 22. XML
      1. 22.1 The to_xml Method
      2. 22.2 The XML Builder
      3. 22.3 Parsing XML
      4. 22.4 Conclusion
    35. Appendix A. Active Model API Reference
      1. A.1 AttributeMethods
      2. A.2 Callbacks
      3. A.3 Conversion
      4. A.4 Dirty
      5. A.5 Errors
      6. A.6 ForbiddenAttributesError
      7. A.7 Lint::Tests
      8. A.8 Model
      9. A.9 Name
      10. A.10 Naming
      11. A.11 SecurePassword
      12. A.12 Serialization
      13. A.13 Serializers::JSON
      14. A.14 Serializers::Xml
      15. A.15 Translation
      16. A.16 Validations
      17. A.17 Validator
    36. Appendix B. Active Support API Reference
      1. B.1 Array
      2. B.2 ActiveSupport::BacktraceCleaner
      3. B.3 Benchmark
      4. B.4 ActiveSupport::Benchmarkable
      5. B.5 BigDecimal
      6. B.6 ActiveSupport::Cache::Store
      7. B.7 ActiveSupport::CachingKeyGenerator
      8. B.8 ActiveSupport::Callbacks
      9. B.9 Class
      10. B.10 ActiveSupport::Concern
      11. B.11 ActiveSupport::Concurrency
      12. B.12 ActiveSupport::Configurable
      13. B.13 Date
      14. B.14 DateTime
      15. B.15 ActiveSupport::Dependencies
      16. B.16 ActiveSupport::Deprecation
      17. B.17 ActiveSupport::DescendantsTracker
      18. B.18 ActiveSupport::Duration
      19. B.19 Enumerable
      20. B.20 ERB::Util
      21. B.21 FalseClass
      22. B.22 File
      23. B.23 Hash
      24. B.24 ActiveSupport::Gzip
      25. B.25 ActiveSupport::HashWithIndifferentAccess
      26. B.26 ActiveSupport::Inflector::Inflections
      27. B.27 Integer
      28. B.28 ActiveSupport::JSON
      29. B.29 Kernel
      30. B.30 ActiveSupport::KeyGenerator
      31. B.31 ActiveSupport::Logger
      32. B.32 ActiveSupport::MessageEncryptor
      33. B.33 ActiveSupport::MessageVerifier
      34. B.34 Module
      35. B.35 ActiveSupport::Multibyte::Chars
      36. B.36 NilClass
      37. B.37 ActiveSupport::Notifications
      38. B.38 Object
      39. B.39 ActiveSupport::OrderedHash
      40. B.40 ActiveSupport::OrderedOptions
      41. B.41 ActiveSupport::PerThreadRegistry
      42. B.42 ActiveSupport::ProxyObject
      43. B.43 ActiveSupport::Railtie
      44. B.44 Range
      45. B.45 Regexp
      46. B.46 ActiveSupport::Rescuable
      47. B.47 String
      48. B.48 ActiveSupport::StringInquirer
      49. B.49 Struct
      50. B.50 ActiveSupport::Subscriber
      51. B.51 Symbol
      52. B.52 ActiveSupport::TaggedLogging
      53. B.53 ActiveSupport::TestCase
      54. B.54 ActiveSupport::Testing::Assertions
      55. B.55 Thread
      56. B.56 Time
      57. B.57 ActiveSupport::TimeWithZone
      58. B.58 ActiveSupport::TimeZone
      59. B.59 TrueClass
      60. B.60 ActiveSupport::XmlMini
    37. Appendix C. Rails Essentials
      1. C.1 Environmental Concerns
      2. C.2 Essential Gems
      3. C.3 Ruby Toolbox
      4. C.4 Screencasts
    38. Index