Ruby on Rails Tutorial
, by Hartl, Michael- ISBN: 9780136702658 | 0136702651
- Cover: Paperback
- Copyright: 7/1/2020
“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 Disney, GitHub, Shopify, 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, Sixth 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 6.
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 while still being 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 GitHub
- Deploy your applications early and often with Heroku
Register your book for convenient access to downloads, updates, and/or corrections as they become available. See inside book for details.
Michael is a graduate of Harvard College, has a Ph.D. in Physics from Caltech, and is an alumnus of the Y Combinator entrepreneur program.
Chapter 2 A toy app 71 2.1 Planning the application . . . . . . . . . . . . . . . . . . . . 72 2.1.1 A toy model for users . . . . . . . . . . . . . . . . . . 76 2.1.2 A toy model for microposts . . . . . . . . . . . . . . 77 2.2 The Users resource . . . . . . . . . . . . . . . . . . . . . . . 78 2.2.1 A user tour . . . . . . . . . . . . . . . . . . . . . . . 81 2.2.2 MVC in action . . . . . . . . . . . . . . . . . . . . . 89 2.2.3 Weaknesses of this Users resource . . . . . . . . . . . 97 2.3 The Microposts resource . . . . . . . . . . . . . . . . . . . . 97 2.3.1 A micropost microtour . . . . . . . . . . . . . . . . . 98 2.3.2 Putting the micro in microposts . . . . . . . . . . . . 104 2.3.3 A user has_many microposts . . . . . . . . . . . . . 106 2.3.4 Inheritance hierarchies . . . . . . . . . . . . . . . . . 109 2.3.5 Deploying the toy app . . . . . . . . . . . . . . . . . 115 2.4 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 2.4.1 What we learned in this chapter . . . . . . . . . . . . 120
Chapter 3 Mostly static pages 121 3.1 Sample app setup . . . . . . . . . . . . . . . . . . . . . . . . 122 3.2 Static pages . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 3.2.1 Generated static pages . . . . . . . . . . . . . . . . . 131 3.2.2 Custom static pages . . . . . . . . . . . . . . . . . . . 140 3.3 Getting started with testing . . . . . . . . . . . . . . . . . . . 141 3.3.1 Our first test . . . . . . . . . . . . . . . . . . . . . . 146 3.3.2 Red . . . . . . . . . . . . . . . . . . . . . . . . . . . 148 3.3.3 Green . . . . . . . . . . . . . . . . . . . . . . . . . . 149 3.3.4 Refactor . . . . . . . . . . . . . . . . . . . . . . . . . 152 3.4 Slightly dynamic pages . . . . . . . . . . . . . . . . . . . . . 154 3.4.1 Testing titles (Red) . . . . . . . . . . . . . . . . . . . 155 3.4.2 Adding page titles (Green) . . . . . . . . . . . . . . . 156 3.4.3 Layouts and embedded Ruby (Refactor) . . . . . . . . 161 3.4.4 Setting the root route . . . . . . . . . . . . . . . . . . 168 3.5 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . 171 3.5.1 What we learned in this chapter . . . . . . . . . . . . 173 3.6 Advanced testing setup . . . . . . . . . . . . . . . . . . . . . 173 3.6.1 minitest reporters . . . . . . . . . . . . . . . . . . . . 174 3.6.2 Automated tests with Guard . . . . . . . . . . . . . . 174
Chapter 4 Rails-flavored Ruby 179 4.1 Motivation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179 4.1.1 Built-in helpers . . . . . . . . . . . . . . . . . . . . . 180 4.1.2 Custom helpers . . . . . . . . . . . . . . . . . . . . . 181 4.2 Strings and methods . . . . . . . . . . . . . . . . . . . . . . . 185 4.2.1 Strings . . . . . . . . . . . . . . . . . . . . . . . . . 187 4.2.2 Objects and message passing . . . . . . . . . . . . . . 191 4.2.3 Method definitions . . . . . . . . . . . . . . . . . . . 195 4.2.4 Back to the title helper . . . . . . . . . . . . . . . . . 197 4.3 Other data structures . . . . . . . . . . . . . . . . . . . . . . 198 4.3.1 Arrays and ranges . . . . . . . . . . . . . . . . . . . 198 4.3.2 Blocks . . . . . . . . . . . . . . . . . . . . . . . . . 203 4.3.3 Hashes and symbols . . . . . . . . . . . . . . . . . . 207 4.3.4 CSS revisited . . . . . . . . . . . . . . . . . . . . . . 212 4.4 Ruby classes . . . . . . . . . . . . . . . . . . . . . . . . . . . 214 4.4.1 Constructors . . . . . . . . . . . . . . . . . . . . . . 214 4.4.2 Class inheritance . . . . . . . . . . . . . . . . . . . . 216 4.4.3 Modifying built-in classes . . . . . . . . . . . . . . . 220 4.4.4 A controller class . . . . . . . . . . . . . . . . . . . . 222 4.4.5 A user class . . . . . . . . . . . . . . . . . . . . . . . 225 4.5 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . 228 4.5.1 What we learned in this chapter . . . . . . . . . . . . 229
Chapter 5 Filling in the layout 231 5.1 Adding some structure . . . . . . . . . . . . . . . . . . . . . 232 5.1.1 Site navigation . . . . . . . . . . . . . . . . . . . . . 232 5.1.2 Bootstrap and custom CSS . . . . . . . . . . . . . . . 244 5.1.3 Partials . . . . . . . . . . . . . . . . . . . . . . . . . 253 5.2 Sass and the asset pipeline . . . . . . . . . . . . . . . . . . . 259 5.2.1 The asset pipeline . . . . . . . . . . . . . . . . . . . . 259 5.2.2 Syntactically awesome stylesheets . . . . . . . . . . . 263 5.3 Layout links . . . . . . . . . . . . . . . . . . . . . . . . . . . 270 5.3.1 Contact page . . . . . . . . . . . . . . . . . . . . . . 271 5.3.2 Rails routes . . . . . . . . . . . . . . . . . . . . . . . 273 5.3.3 Using named routes . . . . . . . . . . . . . . . . . . . 277 5.3.4 Layout link tests . . . . . . . . . . . . . . . . . . . . 278 5.4 User signup: A first step . . . . . . . . . . . . . . . . . . . . 284 5.4.1 Users controller . . . . . . . . . . . . . . . . . . . . . 284 5.4.2 Signup URL . . . . . . . . . . . . . . . . . . . . . . 286 5.5 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . 289 5.5.1 What we learned in this chapter . . . . . . . . . . . . 291
Chapter 6 Modeling users 293 6.1 User model . . . . . . . . . . . . . . . . . . . . . . . . . . . 294 6.1.1 Database migrations . . . . . . . . . . . . . . . . . . 296 6.1.2 The model file . . . . . . . . . . . . . . . . . . . . . 303 6.1.3 Creating user objects . . . . . . . . . . . . . . . . . . 304 6.1.4 Finding user objects . . . . . . . . . . . . . . . . . . 308 6.1.5 Updating user objects . . . . . . . . . . . . . . . . . . 310 6.2 User validations . . . . . . . . . . . . . . . . . . . . . . . . . 312 6.2.1 A validity test . . . . . . . . . . . . . . . . . . . . . . 313 6.2.2 Validating presence . . . . . . . . . . . . . . . . . . . 316 6.2.3 Length validation . . . . . . . . . . . . . . . . . . . . 320 6.2.4 Format validation . . . . . . . . . . . . . . . . . . . . 322 6.2.5 Uniqueness validation . . . . . . . . . . . . . . . . . 329 6.3 Adding a secure password . . . . . . . . . . . . . . . . . . . 339 6.3.1 A hashed password . . . . . . . . . . . . . . . . . . . 339 6.3.2 User has secure password . . . . . . . . . . . . . . . 342 6.3.3 Minimum password standards . . . . . . . . . . . . . 344 6.3.4 Creating and authenticating a user . . . . . . . . . . . 347 6.4 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . 350 6.4.1 What we learned in this chapter . . . . . . . . . . . . 351
Chapter 7 Sign up 353 7.1 Showing users . . . . . . . . . . . . . . . . . . . . . . . . . . 354 7.1.1 Debug and Rails environments . . . . . . . . . . . . . 354 7.1.2 A Users resource . . . . . . . . . . . . . . . . . . . . 362 7.1.3 Debugger . . . . . . . . . . . . . . . . . . . . . . . . 368 7.1.4 A Gravatar image and a sidebar . . . . . . . . . . . . 372 7.2 Signup form . . . . . . . . . . . . . . . . . . . . . . . . . . . 379 7.2.1 Using form_with . . . . . . . . . . . . . . . . . . 382 7.2.2 Signup form HTML . . . . . . . . . . . . . . . . . . 385 7.3 Unsuccessful signups . . . . . . . . . . . . . . . . . . . . . . 390 7.3.1 A working form . . . . . . . . . . . . . . . . . . . . . 390 7.3.2 Strong parameters . . . . . . . . . . . . . . . . . . . 395 7.3.3 Signup error messages . . . . . . . . . . . . . . . . . 398 7.3.4 A test for invalid submission . . . . . . . . . . . . . . 404 7.4 Successful signups . . . . . . . . . . . . . . . . . . . . . . . 407 7.4.1 The finished signup form . . . . . . . . . . . . . . . . 409 7.4.2 The flash . . . . . . . . . . . . . . . . . . . . . . . . 411 7.4.3 The first signup . . . . . . . . . . . . . . . . . . . . . 415 7.4.4 A test for valid submission . . . . . . . . . . . . . . . 419 7.5 Professional-grade deployment . . . . . . . . . . . . . . . . . 422 7.5.1 SSL in production . . . . . . . . . . . . . . . . . . . 422 7.5.2 Production webserver . . . . . . . . . . . . . . . . . . 424 7.5.3 Production database configuration . . . . . . . . . . . 425 7.5.4 Production deployment . . . . . . . . . . . . . . . . . 426 7.6 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . 429 7.6.1 What we learned in this chapter . . . . . . . . . . . . 429
Chapter 8 Basic login 431 8.1 Sessions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 432 8.1.1 Sessions controller . . . . . . . . . . . . . . . . . . . 433 8.1.2 Login form . . . . . . . . . . . . . . . . . . . . . . . 436 8.1.3 Finding and authenticating a user . . . . . . . . . . . 441 8.1.4 Rendering with a flash message . . . . . . . . . . . . 446 8.1.5 A flash test . . . . . . . . . . . . . . . . . . . . . . . 449 8.2 Logging in . . . . . . . . . . . . . . . . . . . . . . . . . . . . 451 8.2.1 The log_in method . . . . . . . . . . . . . . . . . . 452 8.2.2 Current user . . . . . . . . . . . . . . . . . . . . . . . 455 8.2.3 Changing the layout links . . . . . . . . . . . . . . . 460 8.2.4 Testing layout changes . . . . . . . . . . . . . . . . . 468 8.2.5 Login upon signup . . . . . . . . . . . . . . . . . . . 474 8.3 Logging out . . . . . . . . . . . . . . . . . . . . . . . . . . . 477 8.4 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . 481 8.4.1 What we learned in this chapter . . . . . . . . . . . . 482
Chapter 9 Advanced login 483 9.1 Remember me . . . . . . . . . . . . . . . . . . . . . . . . . . 483 9.1.1 Remember token and digest . . . . . . . . . . . . . . 484 9.1.2 Login with remembering . . . . . . . . . . . . . . . . 491 9.1.3 Forgetting users . . . . . . . . . . . . . . . . . . . . . 501 9.1.4 Two subtle bugs . . . . . . . . . . . . . . . . . . . . 504 9.2 “Remember me” checkbox . . . . . . . . . . . . . . . . . . . 509 9.3 Remember tests . . . . . . . . . . . . . . . . . . . . . . . . . 516 9.3.1 Testing the “remember me” box . . . . . . . . . . . . 517 9.3.2 Testing the remember branch . . . . . . . . . . . . . . 523 9.4 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . 527 9.4.1 What we learned in this chapter . . . . . . . . . . . . 527
Chapter 10 Updating, showing, and deleting users 531 10.1 Updating users . . . . . . . . . . . . . . . . . . . . . . . . . 531 10.1.1 Edit form . . . . . . . . . . . . . . . . . . . . . . . . 532 10.1.2 Unsuccessful edits . . . . . . . . . . . . . . . . . . . 540 10.1.3 Testing unsuccessful edits . . . . . . . . . . . . . . . 543 10.1.4 Successful edits (with TDD) . . . . . . . . . . . . . . 544 10.2 Authorization . . . . . . . . . . . . . . . . . . . . . . . . . . 548 10.2.1 Requiring logged-in users . . . . . . . . . . . . . . . 550 10.2.2 Requiring the right user . . . . . . . . . . . . . . . . 556 10.2.3 Friendly forwarding . . . . . . . . . . . . . . . . . . 562 10.3 Showing all users . . . . . . . . . . . . . . . . . . . . . . . . 567 10.3.1 Users index . . . . . . . . . . . . . . . . . . . . . . . 567 10.3.2 Sample users . . . . . . . . . . . . . . . . . . . . . . 574 10.3.3 Pagination . . . . . . . . . . . . . . . . . . . . . . . 577 10.3.4 Users index test . . . . . . . . . . . . . . . . . . . . . 582 10.3.5 Partial refactoring . . . . . . . . . . . . . . . . . . . . 584 10.4 Deleting users . . . . . . . . . . . . . . . . . . . . . . . . . . 586 10.4.1 Administrative users . . . . . . . . . . . . . . . . . . 587 10.4.2 The destroy action . . . . . . . . . . . . . . . . . . 592 10.4.3 User destroy tests . . . . . . . . . . . . . . . . . . . . 595 10.5 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . 599 10.5.1 What we learned in this chapter . . . . . . . . . . . . 601
Chapter 11 Account activation 603 11.1 Account activations resource . . . . . . . . . . . . . . . . . . 605 11.1.1 Account activations controller . . . . . . . . . . . . . 605 11.1.2 Account activation data model . . . . . . . . . . . . . 607 11.2 Account activation emails . . . . . . . . . . . . . . . . . . . . 614 11.2.1 Mailer templates . . . . . . . . . . . . . . . . . . . . 614 11.2.2 Email previews . . . . . . . . . . . . . . . . . . . . . 620 11.2.3 Email tests . . . . . . . . . . . . . . . . . . . . . . . 622 11.2.4 Updating the Users create action . . . . . . . . . . 627 11.3 Activating the account . . . . . . . . . . . . . . . . . . . . . 632 11.3.1 Generalizing the authenticated? method . . . . 632 11.3.2 Activation edit action . . . . . . . . . . . . . . . . 638 11.3.3 Activation test and refactoring . . . . . . . . . . . . . 641 11.4 Email in production . . . . . . . . . . . . . . . . . . . . . . . 649 11.5 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . 653 11.5.1 What we learned in this chapter . . . . . . . . . . . . 653
Chapter 12 Password reset 655 12.1 Password resets resource . . . . . . . . . . . . . . . . . . . . 659 12.1.1 Password resets controller . . . . . . . . . . . . . . . 660 12.1.2 New password resets . . . . . . . . . . . . . . . . . . 663 12.1.3 Password reset create action . . . . . . . . . . . . 666 12.2 Password reset emails . . . . . . . . . . . . . . . . . . . . . . 671 12.2.1 Password reset mailer and templates . . . . . . . . . . 671 12.2.2 Email tests . . . . . . . . . . . . . . . . . . . . . . . 678 12.3 Resetting the password . . . . . . . . . . . . . . . . . . . . . 679 12.3.1 Reset edit action . . . . . . . . . . . . . . . . . . . 680 12.3.2 Updating the reset . . . . . . . . . . . . . . . . . . . 684 12.3.3 Password reset test . . . . . . . . . . . . . . . . . . . 690 12.4 Email in production (take two) . . . . . . . . . . . . . . . . . 696 12.5 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . 698 12.5.1 What we learned in this chapter . . . . . . . . . . . . 700 12.6 Proof of expiration comparison . . . . . . . . . . . . . . . . . 700
Chapter 13 User microposts 703 13.1 A Micropost model . . . . . . . . . . . . . . . . . . . . . . . 703 13.1.1 The basic model . . . . . . . . . . . . . . . . . . . . 704 13.1.2 Micropost validations . . . . . . . . . . . . . . . . . . 707 13.1.3 User/Micropost associations . . . . . . . . . . . . . . 710 13.1.4 Micropost refinements . . . . . . . . . . . . . . . . . 715 13.2 Showing microposts . . . . . . . . . . . . . . . . . . . . . . . 721 13.2.1 Rendering microposts . . . . . . . . . . . . . . . . . 721 13.2.2 Sample microposts . . . . . . . . . . . . . . . . . . . 727 13.2.3 Profile micropost tests . . . . . . . . . . . . . . . . . 734 13.3 Manipulating microposts . . . . . . . . . . . . . . . . . . . . 737 13.3.1 Micropost access control . . . . . . . . . . . . . . . . 738 13.3.2 Creating microposts . . . . . . . . . . . . . . . . . . 742 13.3.3 A proto-feed . . . . . . . . . . . . . . . . . . . . . . 752 13.3.4 Destroying microposts . . . . . . . . . . . . . . . . . 763 13.3.5 Micropost tests . . . . . . . . . . . . . . . . . . . . . 770 13.4 Micropost images . . . . . . . . . . . . . . . . . . . . . . . . 774 13.4.1 Basic image upload . . . . . . . . . . . . . . . . . . . 774 13.4.2 Image validation . . . . . . . . . . . . . . . . . . . . 782 13.4.3 Image resizing . . . . . . . . . . . . . . . . . . . . . 789 13.4.4 Image upload in production . . . . . . . . . . . . . . 794 13.5 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . 802 13.5.1 What we learned in this chapter . . . . . . . . . . . . 805
Chapter 14 Following users 807 14.1 The Relationship model . . . . . . . . . . . . . . . . . . . . . 808 14.1.1 A problem with the data model (and a solution) . . . . 808 14.1.2 User/relationship associations . . . . . . . . . . . . . 818 14.1.3 Relationship validations . . . . . . . . . . . . . . . . 821 14.1.4 Followed users . . . . . . . . . . . . . . . . . . . . . 823 14.1.5 Followers . . . . . . . . . . . . . . . . . . . . . . . . 827 14.2 A web interface for following users . . . . . . . . . . . . . . 830 14.2.1 Sample following data . . . . . . . . . . . . . . . . . 830 14.2.2 Stats and a follow form . . . . . . . . . . . . . . . . . 832 14.2.3 Following and followers pages . . . . . . . . . . . . . 844 14.2.4 A working follow button the standard way . . . . . . . 855 14.2.5 A working follow button with Ajax . . . . . . . . . . 860 14.2.6 Following tests . . . . . . . . . . . . . . . . . . . . . 865 14.3 The status feed . . . . . . . . . . . . . . . . . . . . . . . . . 867 14.3.1 Motivation and strategy . . . . . . . . . . . . . . . . 869 14.3.2 A first feed implementation . . . . . . . . . . . . . . 871 14.3.3 Subselects . . . . . . . . . . . . . . . . . . . . . . . . 875 14.4 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . 881 14.4.1 Guide to further resources . . . . . . . . . . . . . . . 881 14.4.2 What we learned in this chapter . . . . . . . . . . . .
The New copy of this book will include any supplemental materials advertised. Please check the title of the book to determine if it should include any access cards, study guides, lab manuals, CDs, etc.
The Used, Rental and eBook copies of this book are not guaranteed to include any supplemental materials. Typically, only the book itself is included. This is true even if the title states it includes any access cards, study guides, lab manuals, CDs, etc.