Ruby on Rails™ Tutorial

589 Pages · 12.95 mb ·

Michael Hartl

Tutorials

Table of contents

- Contents (Page 8)
- Foreword to the First Edition (Page 16)
- Foreword to the First Edition (Page 18)
- Acknowledgments (Page 20)
- About the Author (Page 22)
- Chapter 1 From Zero to Deploy (Page 24)
- 1.1 Introduction (Page 26)
- 1.1.1 Comments for Various Readers (Page 27)
- 1.1.2 "Scaling" Rails (Page 30)
- 1.1.3 Conventions in This Book (Page 30)
- 1.2 Up and Running (Page 32)
- 1.2.1 Development Environments (Page 33)
- 1.2.2 Ruby, RubyGems, Rails, and Git (Page 35)
- 1.2.3 The First Application (Page 40)
- 1.2.4 Bundler (Page 42)
- 1.2.5 rails server (Page 46)
- 1.2.6 Model-view-controller (MVC) (Page 48)
- 1.3 Version Control with Git (Page 50)
- 1.3.1 Installation and Setup (Page 50)
- 1.3.2 Adding and Committing (Page 53)
- 1.3.3 What Good Does Git Do You? (Page 54)
- 1.3.4 GitHub (Page 55)
- 1.3.5 Branch, Edit, Commit, Merge (Page 57)
- 1.4 Deploying (Page 62)
- 1.4.1 Heroku Setup (Page 62)
- 1.4.2 Heroku Deployment, Step One (Page 63)
- 1.4.3 Heroku Deployment, Step Two (Page 63)
- 1.4.4 Heroku Commands (Page 64)
- 1.5 Conclusion (Page 66)
- Chapter 2 A Demo App (Page 68)
- 2.1 Planning the Application (Page 68)
- 2.1.1 Modeling Demo Users (Page 70)
- 2.1.2 Modeling Demo Microposts (Page 71)
- 2.2 The Users Resource (Page 72)
- 2.2.1 A User Tour (Page 74)
- 2.2.2 MVC in Action (Page 79)
- 2.2.3 Weaknesses of this Users Resource (Page 85)
- 2.3 The Microposts Resource (Page 86)
- 2.3.1 A Micropost Microtour (Page 86)
- 2.3.2 Putting the micro in Microposts (Page 89)
- 2.3.3 A User has_many Microposts (Page 91)
- 2.3.4 Inheritance Hierarchies (Page 93)
- 2.3.5 Deploying the Demo App (Page 96)
- 2.4 Conclusion (Page 97)
- Chapter 3 Mostly Static Pages (Page 100)
- 3.1 Static Pages (Page 105)
- 3.1.1 Truly Static Pages (Page 105)
- 3.1.2 Static Pages with Rails (Page 108)
- 3.2 Our First Tests (Page 116)
- 3.2.1 Test-driven Development (Page 116)
- 3.2.2 Adding a Page (Page 122)
- 3.3 Slightly Dynamic Pages (Page 126)
- 3.3.1 Testing a Title Change (Page 126)
- 3.3.2 Passing Title Tests (Page 129)
- 3.3.3 Embedded Ruby (Page 131)
- 3.3.4 Eliminating Duplication with Layouts (Page 134)
- 3.4 Conclusion (Page 137)
- 3.5 Exercises (Page 137)
- 3.6 Advanced Setup (Page 140)
- 3.6.1 Eliminating bundle exec (Page 141)
- 3.6.2 Automated Tests with Guard (Page 143)
- 3.6.3 Speeding up Tests with Spork (Page 146)
- 3.6.4 Tests inside Sublime Text (Page 150)
- Chapter 4 Rails-Flavored Ruby (Page 152)
- 4.1 Motivation (Page 152)
- 4.2 Strings and Methods (Page 157)
- 4.2.1 Comments (Page 157)
- 4.2.2 Strings (Page 158)
- 4.2.3 Objects and Message Passing (Page 161)
- 4.2.4 Method Definitions (Page 164)
- 4.2.5 Back to the Title Helper (Page 165)
- 4.3 Other Data Structures (Page 165)
- 4.3.1 Arrays and Ranges (Page 165)
- 4.3.2 Blocks (Page 169)
- 4.3.3 Hashes and Symbols (Page 171)
- 4.3.4 CSS revisited (Page 175)
- 4.4 Ruby Classes (Page 176)
- 4.4.1 Constructors (Page 176)
- 4.4.2 Class Inheritance (Page 178)
- 4.4.3 Modifying Built-in Classes (Page 181)
- 4.4.4 A Controller Class (Page 182)
- 4.4.5 A User Class (Page 184)
- 4.5 Conclusion (Page 187)
- 4.6 Exercises (Page 187)
- Chapter 5 Filling in the Layout (Page 190)
- 5.1 Adding Some Structure (Page 190)
- 5.1.1 Site Navigation (Page 192)
- 5.1.2 Bootstrap and Custom CSS (Page 198)
- 5.1.3 Partials (Page 204)
- 5.2 Sass and the Asset Pipeline (Page 210)
- 5.2.1 The Asset Pipeline (Page 210)
- 5.2.2 Syntactically Awesome Stylesheets (Page 213)
- 5.3 Layout Links (Page 220)
- 5.3.1 Route Tests (Page 223)
- 5.3.2 Rails Routes (Page 225)
- 5.3.3 Named Routes (Page 228)
- 5.3.4 Pretty RSpec (Page 230)
- 5.4 User Signup: A First Step (Page 234)
- 5.4.1 Users Controller (Page 235)
- 5.4.2 Signup URI (Page 236)
- 5.5 Conclusion (Page 238)
- 5.6 Exercises (Page 240)
- Chapter 6 Modeling Users (Page 244)
- 6.1 User Model (Page 245)
- 6.1.1 Database Migrations (Page 246)
- 6.1.2 The Model File (Page 251)
- 6.1.3 Creating User Objects (Page 253)
- 6.1.4 Finding User Objects (Page 256)
- 6.1.5 Updating User Objects (Page 258)
- 6.2 User Validations (Page 259)
- 6.2.1 Initial User Tests (Page 259)
- 6.2.2 Validating Presence (Page 262)
- 6.2.3 Length Validation (Page 266)
- 6.2.4 Format Validation (Page 268)
- 6.2.5 Uniqueness Validation (Page 272)
- 6.3 Adding a Secure Password (Page 277)
- 6.3.1 An Encrypted Password (Page 278)
- 6.3.2 Password and Confirmation (Page 280)
- 6.3.3 User Authentication (Page 283)
- 6.3.4 User Has Secure Password (Page 286)
- 6.3.5 Creating a User (Page 288)
- 6.4 Conclusion (Page 290)
- 6.5 Exercises (Page 291)
- Chapter 7 Sign Up (Page 294)
- 7.1 Showing Users (Page 294)
- 7.1.1 Debug and Rails Environments (Page 295)
- 7.1.2 A Users Resource (Page 301)
- 7.1.3 Testing the User Show Page (with Factories) (Page 305)
- 7.1.4 A Gravatar Image and a Sidebar (Page 309)
- 7.2 Signup Form (Page 315)
- 7.2.1 Tests for User Signup (Page 316)
- 7.2.2 Using form_for (Page 320)
- 7.2.3 The Form HTML (Page 324)
- 7.3 Signup Failure (Page 326)
- 7.3.1 A Working Form (Page 326)
- 7.3.2 Signup Error Messages (Page 331)
- 7.4 Signup Success (Page 335)
- 7.4.1 The Finished Signup Form (Page 336)
- 7.4.2 The Flash (Page 338)
- 7.4.3 The First Signup (Page 340)
- 7.4.4 Deploying to Production with SSL (Page 340)
- 7.5 Conclusion (Page 344)
- 7.6 Exercises (Page 344)
- Chapter 8 Sign In, Sign Out (Page 348)
- 8.1 Sessions and Signin Failure (Page 348)
- 8.1.1 Sessions Controller (Page 349)
- 8.1.2 Signin Tests (Page 353)
- 8.1.3 Signin Form (Page 356)
- 8.1.4 Reviewing Form Submission (Page 359)
- 8.1.5 Rendering with a Flash Message (Page 362)
- 8.2 Signin Success (Page 366)
- 8.2.1 Remember Me (Page 366)
- 8.2.2 A Working sign_in Method (Page 372)
- 8.2.3 Current User (Page 374)
- 8.2.4 Changing the Layout Links (Page 378)
- 8.2.5 Signin upon Signup (Page 382)
- 8.2.6 Signing Out (Page 384)
- 8.3 Introduction to Cucumber (Optional) (Page 386)
- 8.3.1 Installation and Setup (Page 387)
- 8.3.2 Features and Steps (Page 388)
- 8.3.3 Counterpoint: RSpec Custom Matchers (Page 391)
- 8.4 Conclusion (Page 394)
- 8.5 Exercises (Page 395)
- Chapter 9 Updating, Showing, and Deleting Users (Page 396)
- 9.1 Updating Users (Page 396)
- 9.1.1 Edit Form (Page 397)
- 9.1.2 Unsuccessful Edits (Page 403)
- 9.1.3 Successful Edits (Page 405)
- 9.2 Authorization (Page 408)
- 9.2.1 Requiring Signed-in Users (Page 409)
- 9.2.2 Requiring the Right User (Page 413)
- 9.2.3 Friendly Forwarding (Page 415)
- 9.3 Showing All Users (Page 419)
- 9.3.1 User Index (Page 419)
- 9.3.2 Sample Users (Page 426)
- 9.3.3 Pagination (Page 427)
- 9.3.4 Partial Refactoring (Page 433)
- 9.4 Deleting Users (Page 436)
- 9.4.1 Administrative Users (Page 436)
- 9.4.2 The destroy Action (Page 440)
- 9.5 Conclusion (Page 445)
- 9.6 Exercises (Page 447)
- Chapter 10 User Microposts (Page 452)
- 10.1 A Micropost Model (Page 452)
- 10.1.1 The Basic Model (Page 453)
- 10.1.2 Accessible Attributes and the First Validation (Page 455)
- 10.1.3 User/Micropost Associations (Page 456)
- 10.1.4 Micropost Refinements (Page 462)
- 10.1.5 Content Validations (Page 466)
- 10.2 Showing Microposts (Page 468)
- 10.2.1 Augmenting the User Show Page (Page 469)
- 10.2.2 Sample Microposts (Page 473)
- 10.3 Manipulating Microposts (Page 477)
- 10.3.1 Access Control (Page 479)
- 10.3.2 Creating Microposts (Page 482)
- 10.3.3 A Proto-feed (Page 490)
- 10.3.4 Destroying Microposts (Page 498)
- 10.4 Conclusion (Page 502)
- 10.5 Exercises (Page 503)
- Chapter 11 Following Users (Page 506)
- 11.1 The Relationship Model (Page 507)
- 11.1.1 A Problem with the Data Model (and a Solution) (Page 508)
- 11.1.2 User/Relationship Associations (Page 514)
- 11.1.3 Validations (Page 518)
- 11.1.4 Followed users (Page 518)
- 11.1.5 Followers (Page 523)
- 11.2 A Web Interface for Following Users (Page 526)
- 11.2.1 Sample Following Data (Page 526)
- 11.2.2 Stats and a Follow Form (Page 528)
- 11.2.3 Following and Followers Pages (Page 538)
- 11.2.4 A Working Follow Button the Standard Way (Page 542)
- 11.2.5 A Working Follow Button with Ajax (Page 547)
- 11.3 The Status Feed (Page 552)
- 11.3.1 Motivation and Strategy (Page 553)
- 11.3.2 A First Feed Implementation (Page 555)
- 11.3.3 Subselects (Page 558)
- 11.3.4 The New Status Feed (Page 561)
- 11.4 Conclusion (Page 562)
- 11.4.1 Extensions to the Sample Application (Page 563)
- 11.4.2 Guide to Further Resources (Page 565)
- 11.5 Exercises (Page 566)
- Index (Page 568)
- A (Page 568)
- B (Page 569)
- C (Page 570)
- D (Page 571)
- E (Page 572)
- F (Page 573)
- G (Page 574)
- H (Page 575)
- I (Page 576)
- J (Page 576)
- K (Page 576)
- L (Page 576)
- M (Page 577)
- N (Page 578)
- O (Page 578)
- P (Page 578)
- R (Page 579)
- S (Page 581)
- T (Page 584)
- U (Page 585)
- V (Page 588)
- W (Page 588)
- Y (Page 589)
- Z (Page 589)