You are previewing Ruby on Rails Tutorial: Learn Web Development with Rails, Third Edition.
O'Reilly logo
Ruby on Rails Tutorial: Learn Web Development with Rails, Third 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 diverse as Twitter, GitHub, Disney, and the Yellow Pages, 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, Third 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, focusing on the fundamental techniques in web development needed for virtually any kind of application. The updates to this edition include simplified installation via a standard development environment in the cloud, use of the default Rails stack throughout, a light-weight testing approach, an all-new section on image upload, and an all-new chapter on account activation and password resets, including sending email with Rails.

This indispensable guide provides integrated tutorials not only for Rails, but also for the essential Ruby, HTML, CSS, and SQL skills you’ll 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 a 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. About This eBook
    2. Title Page
    3. Copyright Page
    4. Praise for Michael Hartl’s Books and Videos on Ruby on Rails
    5. Contents
    6. Foreword to the First Edition
    7. Foreword to the Third Edition
    8. Acknowledgments
    9. About the Author
    10. Chapter 1. From Zero to Deploy
      1. 1.1 Introduction
        1. 1.1.1 Prerequisites
        2. 1.1.2 Conventions 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
        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 Are the Benefits of Using Git?
        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 1
        3. 1.5.3 Heroku Deployment, Step 2
        4. 1.5.4 Heroku Commands
      6. 1.6 Conclusion
        1. 1.6.1 What We Learned in This Chapter
      7. 1.7 Exercises
    11. 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
      5. 2.5 Exercises
    12. Chapter 3. Mostly Static Pages
      1. 3.1 Sample App Setup
      2. 3.2 Static Pages
        1. 3.2.1 Generated Static Pages
        2. 3.2.2 Custom Static Pages
      3. 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
      4. 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
      5. 3.5 Conclusion
        1. 3.5.1 What We Learned in This Chapter
      6. 3.6 Exercises
      7. 3.7 Advanced Testing Setup
        1. 3.7.1 MiniTest Reporters
        2. 3.7.2 Backtrace Silencer
        3. 3.7.3 Automated Tests with Guard
    13. Chapter 4. Rails-Flavored Ruby
      1. 4.1 Motivation
      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
      6. 4.6 Exercises
    14. 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 Style Sheets
      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 Sign-up: A First Step
        1. 5.4.1 Users Controller
        2. 5.4.2 Sign-up URL
      5. 5.5 Conclusion
        1. 5.5.1 What We Learned in This Chapter
      6. 5.6 Exercises
    15. 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 Length
        4. 6.3.4 Creating and Authenticating a User
      4. 6.4 Conclusion
        1. 6.4.1 What We Learned in This Chapter
      5. 6.5 Exercises
    16. 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 Sign-up Form
        1. 7.2.1 Using form_for
        2. 7.2.2 Sign-up Form HTML
      3. 7.3 Unsuccessful Sign-ups
        1. 7.3.1 A Working Form
        2. 7.3.2 Strong Parameters
        3. 7.3.3 Sign-up Error Messages
        4. 7.3.4 A Test for Invalid Submission
      4. 7.4 Successful Sign-ups
        1. 7.4.1 The Finished Sign-up Form
        2. 7.4.2 The Flash
        3. 7.4.3 The First Sign-up
        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 Web Server
        3. 7.5.3 Ruby Version Number
      6. 7.6 Conclusion
        1. 7.6.1 What We Learned in This Chapter
      7. 7.7 Exercises
    17. Chapter 8. Log In, Log Out
      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 Sign-up
      3. 8.3 Logging Out
      4. 8.4 Remember Me
        1. 8.4.1 Remember Token and Digest
        2. 8.4.2 Login with Remembering
        3. 8.4.3 Forgetting Users
        4. 8.4.4 Two Subtle Bugs
        5. 8.4.5 “Remember Me” Check Box
        6. 8.4.6 Remember Tests
      5. 8.5 Conclusion
        1. 8.5.1 What We Learned in This Chapter
      6. 8.6 Exercises
    18. Chapter 9. Updating, Showing, and Deleting Users
      1. 9.1 Updating Users
        1. 9.1.1 Edit Form
        2. 9.1.2 Unsuccessful Edits
        3. 9.1.3 Testing Unsuccessful Edits
        4. 9.1.4 Successful Edits (with TDD)
      2. 9.2 Authorization
        1. 9.2.1 Requiring Logged-in Users
        2. 9.2.2 Requiring the Right User
        3. 9.2.3 Friendly Forwarding
      3. 9.3 Showing All Users
        1. 9.3.1 Users Index
        2. 9.3.2 Sample Users
        3. 9.3.3 Pagination
        4. 9.3.4 Users Index Test
        5. 9.3.5 Partial Refactoring
      4. 9.4 Deleting Users
        1. 9.4.1 Administrative Users
        2. 9.4.2 The destroy Action
        3. 9.4.3 User Destroy Tests
      5. 9.5 Conclusion
        1. 9.5.1 What We Learned in This Chapter
      6. 9.6 Exercises
    19. Chapter 10. Account Activation and Password Reset
      1. 10.1 Account Activation
        1. 10.1.1 Account Activations Resource
        2. 10.1.2 Account Activation Mailer Method
        3. 10.1.3 Activating the Account
        4. 10.1.4 Activation Test and Refactoring
      2. 10.2 Password Reset
        1. 10.2.1 Password Resets Resource
        2. 10.2.2 Password Resets Controller and Form
        3. 10.2.3 Password Reset Mailer Method
        4. 10.2.4 Resetting the Password
        5. 10.2.5 Password Reset Test
      3. 10.3 Email in Production
      4. 10.4 Conclusion
        1. 10.4.1 What We Learned in This Chapter
      5. 10.5 Exercises
      6. 10.6 Proof of Expiration Comparison
    20. Chapter 11. User Microposts
      1. 11.1 A Micropost Model
        1. 11.1.1 The Basic Model
        2. 11.1.2 Micropost Validations
        3. 11.1.3 User/Micropost Associations
        4. 11.1.4 Micropost Refinements
      2. 11.2 Showing Microposts
        1. 11.2.1 Rendering Microposts
        2. 11.2.2 Sample Microposts
        3. 11.2.3 Profile Micropost Tests
      3. 11.3 Manipulating Microposts
        1. 11.3.1 Micropost Access Control
        2. 11.3.2 Creating Microposts
        3. 11.3.3 A Proto-feed
        4. 11.3.4 Destroying Microposts
        5. 11.3.5 Micropost Tests
      4. 11.4 Micropost Images
        1. 11.4.1 Basic Image Upload
        2. 11.4.2 Image Validation
        3. 11.4.3 Image Resizing
        4. 11.4.4 Image Upload in Production
      5. 11.5 Conclusion
        1. 11.5.1 What We Learned in This Chapter
      6. 11.6 Exercises
    21. Chapter 12. Following Users
      1. 12.1 The Relationship Model
        1. 12.1.1 A Problem with the Data Model (and a Solution)
        2. 12.1.2 User/Relationship Associations
        3. 12.1.3 Relationship Validations
        4. 12.1.4 Followed Users
        5. 12.1.5 Followers
      2. 12.2 A Web Interface for Following Users
        1. 12.2.1 Sample Following Data
        2. 12.2.2 Stats and a Follow Form
        3. 12.2.3 Following and Followers Pages
        4. 12.2.4 A Working Follow Button the Standard Way
        5. 12.2.5 A Working Follow Button Created with Ajax
        6. 12.2.6 Following Tests
      3. 12.3 The Status Feed
        1. 12.3.1 Motivation and Strategy
        2. 12.3.2 A First Feed Implementation
        3. 12.3.3 Subselects
      4. 12.4 Conclusion
        1. 12.4.1 Guide to Further Resources
        2. 12.4.2 What We Learned in This Chapter
      5. 12.5 Exercises
    22. Index
    23. Code Snippets