The Rails 7 Way
The Rails 7 Way
About the Book
With a foreword written by Eileen M. Uchitelle, Rails Core Team
“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™ 7 Way is the comprehensive, authoritative reference guide for professionals delivering production-quality code using modern Ruby on Rails. It illuminates the entire Rails 7 API, its most powerful idioms, design approaches, and libraries. Building on the previous editions, this edition has been heavily refactored and updated. It features entirely new chapters on Action Mailbox, Active Storage, asset pipelines, ViewComponent, and Turbo.
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 your database schema via Migrations
- Craft maintainable front-ends with ActionView
- Build your assets with one of the asset pipelines
- Optimize performance and scalability with caching and Turbo
- Improve your productivity using Haml templating
- Secure your systems against attacks like SQL Injection, XSS, and XSRF
- Integrate email using Action Mailer and Action Mailbox
- Improve responsiveness with background processing
- Build “API-only” back-end projects that speak JSON
- Store your files in the cloud via Active Storage
- Leverage Multiple Databases
- Write a reactive application with little to no JavaScript
Table of Contents
- Foreword
- About the Authors
- Acknowledgments
-
Introduction
- About This Book
- Voices from other experts
- Goals
- Prerequisites
- Book Structure
- Licenses, Attributions and Trademark Notice
-
Rails Configuration and Environments
- Generating a new Rails Application
- Bundler
- RSpec and Haml
- Running a Rails application
- Default Initializers
- Other Common Settings
- Zeitwerk
- Development Mode
- Test Mode
- Production Mode
- Configuring Application Secrets
- Configuring a Database
- Logging
- Default Gems
- Rack
- Conclusion
-
Routing
- The Two Purposes of Routing
-
The
routes.rb
File - Named Routes
- Scoping Routing Rules
- Listing Routes
- Conclusion
-
REST, Resources, and Rails
- REST in a Rather Small Nutshell
- Resources and Representations
- REST in Rails
- Routing and CRUD
- The Standard RESTful Controller Actions
- Singular Resource Routes
- Nested Resources
- Routing Concerns
- RESTful Route Customizations
- Controller-Only Resources
- Different Representations of Resources
- The RESTful Rails Action Set
- Conclusion
-
Working with Controllers
- Action Dispatch: Where It All Begins
- Parameters
- Render onto View…
- Additional Layout Options
- Redirecting
- Controller/View Communication
- Action Callbacks
- Streaming
-
The
respond_to
Method - Conclusion
-
Cookies, Session Management and the Flash
- Cookies
- Session
- The Flash
- What to Store in a Cookie
- What to Store in the Session
- Conclusion
-
Action View & Haml
- Haml
- Layouts and Templates
- Partials
- Conclusion
-
Active Record Migrations
- Creating Migrations
- Defining Columns
- Data Migration
- Database Schema and Sequencing
- Database Seeding
- Database-Related Tasks
- Conclusion
-
Working with Active Record
- The Basics
- Macro-Style Methods
- Defining Attributes
- CRUD: Creating, Reading, Updating, Deleting
- Database Locking
- Ignoring Columns
- Connections to Multiple Databases
- Using the Database Connection Directly
- Convention over Configuration
- Conclusion
-
Active Record Associations
- The Association Hierarchy
- One-to-Many Relationships
- Belongs to Associations
- Has Many Associations
- Many-to-Many Relationships
- One-to-One Relationships
- Working with Unsaved Objects and Associations
- Association Extensions
-
The
CollectionProxy
Class - Conclusion
-
Working with Queries
- Query Methods
- Custom SQL Queries
- Scopes
- Query Log Tags
- Conclusion
-
Validations
- Finding Errors
- Included Validations
- Common Validation Options
- Conditional Validation
- Short-form Validation
- Custom Validation Techniques
- Skipping Validations
- Working with the Errors object
- Conclusion
-
Advanced Active Record
-
Polymorphic
has_many
Relationships - Modules for Reusing Common Behavior
- Delegated Types
- Non-Persisted Models
- Callbacks
- Attributes API
- Serialized Attributes
- Enums
- Generating Secure Tokens
- Calculation Methods
- Batch Operations
- Single-Table Inheritance (STI)
- Abstract Base Model Classes
- Foreign-key Constraints
- Value Objects
- Modifying Active Record Classes at Runtime
- PostgreSQL
- Other Connection Methods
- Conclusion
-
Polymorphic
-
Forms
-
The basics of
form_with
-
FormOptionsHelper
- The Date and Time Selection Helpers
- Conclusion
-
The basics of
-
Internationalization
- Localized Views
- I18n Setup
- Setting and Passing the Locale
- Setting Locale from Client Supplied Information
- Internationalizing Your Application
- Organization of Locale Files
- Looking Up Translations
- How to Store Your Custom Translations
- Exception Handling
- Conclusion
-
All About Helpers
-
AssetTagHelper
-
AssetUrlHelper
-
AtomFeedHelper
-
CacheHelper
-
CaptureHelper
-
ControllerHelper
-
CspHelper
-
CsrfHelper
-
DateHelper
-
DebugHelper
-
FormHelper
,FormOptionsHelper
andFormTagHelper
-
JavaScriptHelper
-
NumberHelper
-
OutputSafetyHelper
-
RenderingHelper
-
SanitizeHelper
-
TagHelper
-
TextHelper
-
TranslationHelper
-
UrlHelper
- Writing Your Own View Helpers
- Wrapping and Generalizing Partials
- Conclusion
-
-
Authentication and Authorization
- Warden
- Devise
-
has_secure_password
- Pundit
- Conclusion
-
Security
- Password Management
- Log Masking
- TLS (Transport Layer Security)
- Model Mass-assignment Attributes Protection
- SQL Injection
- Cross-Site Scripting (XSS)
- CSRF (Cross-Site Request Forgery)
- Content Security Policy
- Permissions Policy
- Session Fixation Attacks
- Conclusion
-
Caching and Performance
- View Caching
- Data Caching
- Control of Web Caching
- ETags
- Conclusion
-
RSpec
- Introduction
- Behavior-Driven Development
- Basic Syntax and API
- Custom Expectation Matchers
- Helper Methods
- Shared Behaviors
- Shared Context
- Test Doubles
- Running Specs
- factory_bot
- Testing Tool Belt
- Writing Specs for your Rails App
- Conclusion
-
Background Processing
- Active Job
- Queueing Backends
- Conclusion
-
Active Storage
- Setup
- Expense Report
- Representations and Variants
- Removing Files
- Production
- Conclusion
-
Action Mailer and Action Mailbox
- Mailer Models
- Previews
- Testing Email Content
- Configuration
- Receiving Emails
- Conclusion
-
Asset Pipeline
- Propshaft
- importmap-rails
- dartsass-rails
- tailwindcss-rails
- A workflow without Node, npm or yarn
- CSS & JS Bundling
- The Rails Core Team Options
- faucet-pipeline
- Assets in Production
- Conclusion
-
Component-Based Views
- Installation and Configuration
- Bootstrap
- Styling the Flash
- Component Helper
- Closing the Alert
- Slots
- Rendering Collections
- Previews
- Working with Components
- Adding Items to our Invoice
- Conclusion
-
JavaScript
- Turbo Drive
- Turbo Frames
- Turbo Streams
- Broadcasting TurboStreams
- Custom Elements
- Alternatives
- Conclusion
- Notes
The Leanpub 60 Day 100% Happiness Guarantee
Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.
Now, this is technically risky for us, since you'll have the book or course files either way. But we're so confident in our products and services, and in our authors and readers, that we're happy to offer a full money back guarantee for everything we sell.
You can only find out how good something is by trying it, and because of our 100% money back guarantee there's literally no risk to do so!
So, there's no reason not to click the Add to Cart button, is there?
See full terms...
Earn $8 on a $10 Purchase, and $16 on a $20 Purchase
We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.
(Yes, some authors have already earned much more than that on Leanpub.)
In fact, authors have earnedover $13 millionwriting, publishing and selling on Leanpub.
Learn more about writing on Leanpub
Free Updates. DRM Free.
If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).
Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.
Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.
Learn more about Leanpub's ebook formats and where to read them