You are previewing Ruby on Rails™ Tutorial: Learn Web Development with Rails, Fourth Edition.
O'Reilly logo
Ruby on Rails™ Tutorial: Learn Web Development with Rails, Fourth Edition

Book Description

Ruby on Rails™ Tutorial by Michael Hartl has become a must-read for developers learning how to build Rails apps.” — Peter Cooper, Editor of Ruby Inside

Used by sites as varied as Twitter, GitHub, Disney, and Airbnb, Ruby on Rails is one of the most popular frameworks for developing web applications, but it can be challenging to learn and use. Whether you’re new to web development or new only to Rails, Ruby on Rails™ Tutorial, Fourth Edition, is the solution.

Best-selling author and leading Rails developer Michael Hartl teaches Rails by guiding you through the development of three example applications of increasing sophistication. The tutorial’s examples focus on the general principles of web development needed for virtually any kind of website. The updates to this edition include full compatibility with Rails 5, a division of the largest chapters into more manageable units, and a huge number of new exercises interspersed in each chapter for maximum reinforcement of the material.

This indispensable guide provides integrated tutorials not only for Rails, but also for the essential Ruby, HTML, CSS, and SQL skills you need when developing web applications. Hartl explains how each new technique solves a real-world problem, and then he demonstrates it with bite-sized code that’s simple enough to understand, yet novel enough to be useful. Whatever your previous web development experience, this book will guide you to true Rails mastery.

This book will help you

  • Install and set up your Rails development environment, including pre-installed integrated development environment (IDE) in the cloud

  • Go beyond generated code to truly understand how to build Rails applications from scratch

  • Learn testing and test-driven development (TDD)

  • Effectively use the Model-View-Controller (MVC) pattern

  • Structure applications using the REST architecture

  • Build static pages and transform them into dynamic ones

  • Master the Ruby programming skills all Rails developers need

  • Create high-quality site layouts and data models

  • Implement registration and authentication systems, including validation and secure passwords

  • Update, display, and delete users

  • Upload images in production using a cloud storage service

  • Implement account activation and password reset, including sending email with Rails

  • Add social features and microblogging, including an introduction to Ajax

  • Record version changes with Git and create a secure remote repository at Bitbucket

  • Deploy your applications early and often with Heroku

  • Table of Contents

    1. Cover Page
    2. Title Page
    3. Copyright Page
    4. Contents
    5. Foreword to the First Edition by Derek Sivers
    6. Foreword to the Third Edition by Obie Fernandez
    7. Acknowledgments
    8. About the Author
    9. Chapter 1 From Zero to Deploy
      1. 1.1 Introduction
        1. 1.1.1 Prerequisites
        2. 1.1.2 Conventions Used in This Book
      2. 1.2 Up and Running
        1. 1.2.1 Development Environment
        2. 1.2.2 Installing Rails
      3. 1.3 The First Application
        1. 1.3.1 Bundler
        2. 1.3.2 rails server
        3. 1.3.3 Model-View-Controller (MVC)
        4. 1.3.4 Hello, World!
      4. 1.4 Version Control with Git
        1. 1.4.1 Installation and Setup
        2. 1.4.2 What Good Does Git Do You?
        3. 1.4.3 Bitbucket
        4. 1.4.4 Branch, Edit, Commit, Merge
      5. 1.5 Deploying
        1. 1.5.1 Heroku Setup
        2. 1.5.2 Heroku Deployment, Step One
        3. 1.5.3 Heroku Deployment, Step Two
        4. 1.5.4 Heroku Commands
      6. 1.6 Conclusion
        1. 1.6.1 What We Learned in This Chapter
    10. Chapter 2 A Toy App
      1. 2.1 Planning the Application
        1. 2.1.1 A Toy Model for Users
        2. 2.1.2 A Toy Model for Microposts
      2. 2.2 The Users Resource
        1. 2.2.1 A User Tour
        2. 2.2.2 MVC in Action
        3. 2.2.3 Weaknesses of this Users Resource
      3. 2.3 The Microposts Resource
        1. 2.3.1 A Micropost Microtour
        2. 2.3.2 Putting the Micro in Microposts
        3. 2.3.3 A User has many Microposts
        4. 2.3.4 Inheritance Hierarchies
        5. 2.3.5 Deploying the Toy App
      4. 2.4 Conclusion
        1. 2.4.1 What We Learned in This Chapter
    11. Chapter 3 Mostly Static Pages
      1. 3.1 Sample App Setup
        1. 3.2 Static Pages
        2. 3.2.1 Generated Static Pages
        3. 3.2.2 Custom Static Pages
      2. 3.3 Getting Started with Testing
        1. 3.3.1 Our First Test
        2. 3.3.2 Red
        3. 3.3.3 Green
        4. 3.3.4 Refactor
      3. 3.4 Slightly Dynamic Pages
        1. 3.4.1 Testing Titles (Red)
        2. 3.4.2 Adding Page Titles (Green)
        3. 3.4.3 Layouts and Embedded Ruby (Refactor)
        4. 3.4.4 Setting the Root Route
      4. 3.5 Conclusion
        1. 3.5.1 What We Learned in This Chapter
      5. 3.6 Advanced Testing Setup
        1. 3.6.1 Minitest Reporters
        2. 3.6.2 Automated Tests with Guard
    12. Chapter 4 Rails-Flavored Ruby
      1. 4.1 Motivation
        1. 4.1.1 Built-In Helpers
        2. 4.1.2 Custom Helpers
      2. 4.2 Strings and Methods
        1. 4.2.1 Comments
        2. 4.2.2 Strings
        3. 4.2.3 Objects and Message Passing
        4. 4.2.4 Method Definitions
        5. 4.2.5 Back to the Title Helper
      3. 4.3 Other Data Structures
        1. 4.3.1 Arrays and Ranges
        2. 4.3.2 Blocks
        3. 4.3.3 Hashes and Symbols
        4. 4.3.4 CSS Revisited
      4. 4.4 Ruby Classes
        1. 4.4.1 Constructors
        2. 4.4.2 Class Inheritance
        3. 4.4.3 Modifying Built-in Classes
        4. 4.4.4 A Controller Class
        5. 4.4.5 A User Class
      5. 4.5 Conclusion
        1. 4.5.1 What We Learned in This Chapter
    13. Chapter 5 Filling in the Layout
      1. 5.1 Adding Some Structure
        1. 5.1.1 Site Navigation
        2. 5.1.2 Bootstrap and Custom CSS
        3. 5.1.3 Partials
      2. 5.2 Sass and the Asset Pipeline
        1. 5.2.1 The Asset Pipeline
        2. 5.2.2 Syntactically Awesome Stylesheets
      3. 5.3 Layout Links
        1. 5.3.1 Contact Page
        2. 5.3.2 Rails Routes
        3. 5.3.3 Using Named Routes
        4. 5.3.4 Layout Link Tests
      4. 5.4 User Signup: A First Step
        1. 5.4.1 Users Controller
        2. 5.4.2 Signup URL
      5. 5.5 Conclusion
        1. 5.5.1 What We Learned in This Chapter
    14. Chapter 6 Modeling Users
      1. 6.1 User Model
        1. 6.1.1 Database Migrations
        2. 6.1.2 The Model File
        3. 6.1.3 Creating User Objects
        4. 6.1.4 Finding User Objects
        5. 6.1.5 Updating User Objects
      2. 6.2 User Validations
        1. 6.2.1 A Validity Test
        2. 6.2.2 Validating Presence
        3. 6.2.3 Length Validation
        4. 6.2.4 Format Validation
        5. 6.2.5 Uniqueness Validation
      3. 6.3 Adding a Secure Password
        1. 6.3.1 A Hashed Password
        2. 6.3.2 User Has Secure Password
        3. 6.3.3 Minimum Password Standards
        4. 6.3.4 Creating and Authenticating a User
      4. 6.4 Conclusion
        1. 6.4.1 What We Learned in This Chapter
    15. Chapter 7 Sign Up
      1. 7.1 Showing Users
        1. 7.1.1 Debug and Rails Environments
        2. 7.1.2 A Users Resource
        3. 7.1.3 Debugger
        4. 7.1.4 A Gravatar Image and a Sidebar
      2. 7.2 Signup Form
        1. 7.2.1 Using form_for
        2. 7.2.2 Signup form HTML
      3. 7.3 Unsuccessful Signups
        1. 7.3.1 A Working Form
        2. 7.3.2 Strong Parameters
        3. 7.3.3 Signup Error Messages
        4. 7.3.4 A Test for Invalid Submission
      4. 7.4 Successful Signups
        1. 7.4.1 The Finished Signup Form
        2. 7.4.2 The Flash
        3. 7.4.3 The First Signup
        4. 7.4.4 A Test for Valid Submission
      5. 7.5 Professional-Grade Deployment
        1. 7.5.1 SSL in Production
        2. 7.5.2 Production Webserver
        3. 7.5.3 Production Deployment
      6. 7.6 Conclusion
        1. 7.6.1 What We Learned in This Chapter
    16. Chapter 8 Basic Login
      1. 8.1 Sessions
        1. 8.1.1 Sessions Controller
        2. 8.1.2 Login Form
        3. 8.1.3 Finding and Authenticating a User
        4. 8.1.4 Rendering with a Flash Message
        5. 8.1.5 A Flash Test
      2. 8.2 Logging In
        1. 8.2.1 The log_in Method
        2. 8.2.2 Current User
        3. 8.2.3 Changing the Layout Links
        4. 8.2.4 Testing Layout Changes
        5. 8.2.5 Login Upon Signup
      3. 8.3 Logging Out
      4. 8.4 Conclusion
        1. 8.4.1 What We Learned in This Chapter
    17. Chapter 9 Advanced Login
      1. 9.1 Remember Me
        1. 9.1.1 Remember Token and Digest
        2. 9.1.2 Login with Remembering
        3. 9.1.3 Forgetting Users
        4. 9.1.4 Two Subtle Bugs
      2. 9.2 “Remember Me” Checkbox
      3. 9.3 Remember Tests
        1. 9.3.1 Testing the “Remember Me” Box
        2. 9.3.2 Testing the Remember Branch
      4. 9.4 Conclusion
        1. 9.4.1 What We Learned in This Chapter
    18. Chapter 10 Updating, Showing, and Deleting Users
      1. 10.1 Updating Users
        1. 10.1.1 Edit Form
        2. 10.1.2 Unsuccessful Edits
        3. 10.1.3 Testing Unsuccessful Edits
        4. 10.1.4 Successful Edits (with TDD)
      2. 10.2 Authorization
        1. 10.2.1 Requiring Logged-in Users
        2. 10.2.2 Requiring the Right User
        3. 10.2.3 Friendly Forwarding
      3. 10.3 Showing All Users
        1. 10.3.1 Users Index
        2. 10.3.2 Sample Users
        3. 10.3.3 Pagination
        4. 10.3.4 Users Index Test
        5. 10.3.5 Partial Refactoring
      4. 10.4 Deleting Users
        1. 10.4.1 Administrative Users
        2. 10.4.2 The destroy Action
        3. 10.4.3 User Destroy Tests
      5. 10.5 Conclusion
        1. 10.5.1 What We Learned in This Chapter
    19. Chapter 11 Account Activation
      1. 11.1 Account Activations Resource
        1. 11.1.1 Account Activations Controller
        2. 11.1.2 Account Activation Data Model
      2. 11.2 Account Activation Emails
        1. 11.2.1 Mailer Templates
        2. 11.2.2 Email Previews
        3. 11.2.3 Email Tests
        4. 11.2.4 Updating the Users create Action
      3. 11.3 Activating the Account
        1. 11.3.1 Generalizing the authenticated? Method
        2. 11.3.2 Activation edit Action
        3. 11.3.3 Activation Test and Refactoring
      4. 11.4 Email in Production
        1. 11.5 Conclusion
        2. 11.5.1 What We Learned in This Chapter
    20. Chapter 12 Password Reset
      1. 12.1 Password Resets Resource
        1. 12.1.1 Password Resets Controller
        2. 12.1.2 New Password Resets
        3. 12.1.3 Password Reset create Action
      2. 12.2 Password Reset Emails
        1. 12.2.1 Password Reset Mailer and Templates
        2. 12.2.2 Email Tests
      3. 12.3 Resetting the Password
        1. 12.3.1 Reset edit Action
        2. 12.3.2 Updating the Reset
        3. 12.3.3 Password Reset Test
      4. 12.4 Email in Production (Take Two)
      5. 12.5 Conclusion
        1. 12.5.1 What We Learned in This Chapter
      6. 12.6 Proof of Expiration Comparison
    21. Chapter 13 User Microposts
      1. 13.1 A Micropost Model
        1. 13.1.1 The Basic Model
        2. 13.1.2 Micropost Validations
        3. 13.1.3 User/Micropost Associations
        4. 13.1.4 Micropost Refinements
      2. 13.2 Showing Microposts
        1. 13.2.1 Rendering Microposts
        2. 13.2.2 Sample Microposts
        3. 13.2.3 Profile Micropost Tests
      3. 13.3 Manipulating Microposts
        1. 13.3.1 Micropost Access Control
        2. 13.3.2 Creating Microposts
        3. 13.3.3 A Proto-Feed
        4. 13.3.4 Destroying Microposts
        5. 13.3.5 Micropost Tests
      4. 13.4 Micropost Images
        1. 13.4.1 Basic Image Upload
        2. 13.4.2 Image Validation
        3. 13.4.3 Image Resizing
        4. 13.4.4 Image Upload in Production
      5. 13.5 Conclusion
        1. 13.5.1 What We Learned in This Chapter
    22. Chapter 14 Following Users
      1. 14.1 The Relationship Model
        1. 14.1.1 A Problem with the Data Model (and a Solution)
        2. 14.1.2 User/Relationship Associations
        3. 14.1.3 Relationship Validations
        4. 14.1.4 Followed Users
        5. 14.1.5 Followers
      2. 14.2 A Web Interface for Following Users
        1. 14.2.1 Sample Following Data
        2. 14.2.2 Stats and a Follow Form
        3. 14.2.3 Following and Followers Pages
        4. 14.2.4 A Working Follow Button the Standard Way
        5. 14.2.5 A Working Follow Button with Ajax
        6. 14.2.6 Following Tests
      3. 14.3 The Status Feed
        1. 14.3.1 Motivation and Strategy
        2. 14.3.2 A First Feed Implementation
        3. 14.3.3 Subselects
      4. 14.4 Conclusion
        1. 14.4.1 Guide to Further Resources
        2. 14.4.2 What We Learned in This Chapter
    23. Index