Obie Fernandez, Kevin Faustino — «The Rails 4
Total Page:16
File Type:pdf, Size:1020Kb
The Rails 4 Way Obie Fernandez, Kevin Faustino and Vitaly Kushner This book is for sale at http://leanpub.com/tr4w This version was published on 2014-06-03 This is a Leanpub book. Leanpub empowers authors and publishers with the Lean Publishing process. Lean Publishing is the act of publishing an in-progress ebook using lightweight tools and many iterations to get reader feedback, pivot until you have the right book and build traction once you do. ©2013 - 2014 Obie Fernandez, Kevin Faustino and Vitaly Kushner Tweet This Book! Please help Obie Fernandez, Kevin Faustino and Vitaly Kushner by spreading the word about this book on Twitter! The suggested tweet for this book is: I just bought the beta release of The Rails 4 Way The suggested hashtag for this book is #tr4w. Find out what other people are saying about the book by clicking on this link to search for this hashtag on Twitter: https://twitter.com/search?q=#tr4w Taylor, your hard work and dedication to your craft is an inspiration to me every day. I love you… Contents Foreword ................................................... ii Foreword (to The Rails 3 Way) ....................................... iii Foreword (to The Rails Way) ........................................ v Acknowledgments .............................................. vi About the Authors .............................................. vii Obie Fernandez .............................................. vii Kevin Faustino .............................................. vii Introduction ................................................. viii About This Book ............................................. viii Recommended Reading and Resources ................................. ix Goals ................................................... x Prerequisites ................................................ xi Required Technology ........................................... xi 1 Rails Environments and Configuration ................................ 1 1.1 Bundler ............................................... 1 1.2 Startup and Application Settings ................................. 8 1.3 Development Mode ........................................ 16 1.4 Test Mode .............................................. 20 1.5 Production Mode .......................................... 21 1.6 Configuring a Database ...................................... 24 1.7 Configuring Application Secrets .................................. 26 1.8 Logging ............................................... 27 2 Routing .................................................. 34 2.1 The Two Purposes of Routing ................................... 34 2.2 The routes.rb File .......................................... 35 2.3 Route Globbing ........................................... 47 2.4 Named Routes ........................................... 48 2.5 Scoping Routing Rules ....................................... 53 2.6 Listing Routes ........................................... 55 2.7 Conclusion ............................................. 56 CONTENTS 3 REST, Resources, and Rails ....................................... 57 3.1 REST in a Rather Small Nutshell ................................. 57 3.2 Resources and Representations .................................. 58 3.3 REST in Rails ............................................ 58 3.4 Routing and CRUD ......................................... 59 3.5 The Standard RESTful Controller Actions ............................ 62 3.6 Singular Resource Routes ..................................... 65 3.7 Nested Resources .......................................... 66 3.8 Routing Concerns ......................................... 70 3.9 RESTful Route Customizations .................................. 71 3.10 Controller-Only Resources ..................................... 75 3.11 Different Representations of Resources .............................. 77 3.12 The RESTful Rails Action Set ................................... 79 3.13 Conclusion ............................................. 83 4 Working with Controllers ....................................... 85 4.1 Rack ................................................. 85 4.2 Action Dispatch: Where It All Begins ............................... 88 4.3 Render unto View… ........................................ 91 4.4 Additional Layout Options ..................................... 99 4.5 Redirecting ............................................. 100 4.6 Controller/View Communication ................................. 103 4.7 Action Callbacks .......................................... 104 4.8 Streaming .............................................. 109 4.9 Variants ............................................... 114 4.10 Conclusion ............................................. 115 5 Working with Active Record ...................................... 117 5.1 The Basics ............................................. 117 5.2 Macro-Style Methods ....................................... 118 5.3 Defining Attributes ......................................... 121 5.4 CRUD: Creating, Reading, Updating, Deleting . 125 5.5 Database Locking ......................................... 138 5.6 Where Clauses ........................................... 141 5.7 Connections to Multiple Databases in Different Models . 152 5.8 Using the Database Connection Directly ............................. 153 5.9 Other Configuration Options ................................... 157 5.10 Conclusion ............................................. 157 6 Active Record Migrations ........................................ 159 6.1 Creating Migrations ........................................ 159 6.2 Data Migration ........................................... 172 6.3 schema.rb ............................................. 174 6.4 Database Seeding .......................................... 174 6.5 Database-Related Rake Tasks ................................... 175 6.6 Conclusion ............................................. 179 CONTENTS 7 Active Record Associations ....................................... 180 7.1 The Association Hierarchy ..................................... 180 7.2 One-to-Many Relationships .................................... 181 7.3 The belongs_to Association .................................... 189 7.4 The has_many Association ..................................... 197 7.5 Many-to-Many Relationships ................................... 205 7.6 One-to-One Relationships ..................................... 215 7.7 Working with Unsaved Objects and Associations . 219 7.8 Association Extensions ....................................... 220 7.9 The CollectionProxy Class .................................... 221 7.10 Conclusion ............................................. 222 8 Validations ................................................ 224 8.1 Finding Errors ........................................... 224 8.2 The Simple Declarative Validations ................................ 224 8.3 Common Validation Options ................................... 234 8.4 Conditional Validation ....................................... 236 8.5 Short-form Validation ....................................... 238 8.6 Custom Validation Techniques .................................. 239 8.7 Skipping Validations ........................................ 242 8.8 Working with the Errors Hash ................................... 242 8.9 Testing Validations with Shoulda ................................. 243 8.10 Conclusion ............................................. 244 9 Advanced Active Record ........................................ 245 9.1 Scopes ................................................ 245 9.2 Callbacks .............................................. 250 9.3 Calculation Methods ........................................ 259 9.4 Single-Table Inheritance (STI) ................................... 261 9.5 Abstract Base Model Classes .................................... 267 9.6 Polymorphic has_many Relationships ............................... 268 9.7 Enums ............................................... 272 9.8 Foreign-key Constraints ...................................... 274 9.9 Modules for Reusing Common Behavior ............................. 274 9.10 Modifying Active Record Classes at Runtime . 278 9.11 Using Value Objects ........................................ 280 9.12 Non-Persisted Models ....................................... 283 9.13 PostgreSQL enhancements ..................................... 285 9.14 Conclusion ............................................. 292 10 Action View ............................................... 293 10.1 Layouts and Templates ....................................... 293 10.2 Partials ............................................... 302 10.3 Conclusion ............................................. 308 11 All About Helpers ............................................ 309 CONTENTS 11.1 ActiveModelHelper ........................................ 309 11.2 AssetTagHelper .......................................... 315 11.3 AtomFeedHelper .......................................... 323 11.4 CacheHelper ............................................ 325 11.5 CaptureHelper ........................................... 325 11.6 CsrfHelper ............................................. 326 11.7 DateHelper ............................................. 327 11.8 DebugHelper ............................................ 333 11.9 FormHelper ............................................. 333 11.10 FormOptionsHelper .......................................