Table of Contents
Foreword xi
Preface xiii
1 Starting Up Ruby on Rails 1
If You Run Windows, You're Lucky 2
Getting Started at the Command Line 3
Starting Up Rails 9
Test Your Knowledge 11
Quiz 11
Answers 11
2 Rails on the Web 13
Creating Your Own View 13
What Are All Those Folders? 17
Adding Some Data 18
How Hello World Works 20
Adding Logic to the View 22
Test Your Knowledge 24
Quiz 24
Answers 24
3 Adding Web Style 27
I Want My CSS! 27
Specifying Stylesheets 33
Creating a Layout for a Controller 34
Choosing a Layout from a Controller 36
Sharing Template Data with the Layout 38
Setting a Default Page 39
Test Your Knowledge 40
Quiz 40
Answers 40
4 Managing Data Flow: Controllers and Models 43
Getting Started, Greeting Guests 43
Application Flow 48
Keeping Track: A Simple Guestbook 49
Connecting to a Database Through a Model 50
Connecting the Controller to the Model 53
Finding Data with ActiveRecord 57
Test Your Knowledge 61
Quiz 61
Answers 62
5 Accelerating Development with Scaffolding and REST 63
A First Look at Scaffolding 63
REST and Controller Best Practices 68
Websites and Web Applications 68
Toward a Cleaner Approach 70
Examining a RESTful Controller 71
Index: An Overview of Data 76
Show: Just One Row of Data and the Before Filter 77
New: A Blank Set of Data Fields 78
Edit: Fiana Me That Data, Please 78
Create: Save Something New 79
Put This Updated Record In 81
Destroy It 82
Escaping the REST Prison 83
Test Your Knowledge 84
Quiz 84
Answers 84
6 Presenting Models with Forms 87
More Than a Name on a Form 87
Generating HTML Forms with Scaffolding 89
Form as a Wrapper 94
Creating Text Fields and Text Areas 96
Labels 98
Creating Checkboxes 99
Creating Radio Buttons 100
Creating Selection Lists 103
Dates and Times 105
Creating Helper Methods 107
Test Your Knowledge 110
Quiz 110
Answers 110
7 Strengthening Models with Validation 113
Without Validation 113
The Original Model 116
The Power of Declarative Validation 117
Managing Secrets 120
Customizing the Message 120
Limiting Choices 122
Testing Format with Regular Expressions 122
Seen It All Before 123
Numbers Only 123
A Place on the Calendar 125
Testing for Presence 125
Beyond Simple Declarations 126
Test It Only If 126
Do It Yourself 127
Test Your Knowledge 128
Quiz 128
Answer 129
8 Improving Forms 131
Adding a Picture by Uploading a File 131
File Upload Forms 132
Model and Migration Changes 133
Results 138
Standardizing Your Look with Form Builders 143
Supporting Your Own Field Types 143
Adding Automation 145
Integrating Form Builders and Styles 148
Test Your Knowledge 151
Quiz 151
Answers 152
9 Developing Model Relationships 153
Connecting Awards to Students 154
Establishing the Relationship 155
Supporting the Relationship 156
Connecting Students to Awards 160
Removing Awards When Students Disappear 160
Counting Awards for Students 160
Nesting Awards in Students 162
Changing the Routing 163
Changing the Controller 164
Changing the Award Views 166
Connecting the Student Views 171
Is Nesting Worth It? 172
Many-to-Many: Connecting Students to Courses 173
Creating Tables 173
Connecting the Models 174
Adding to the Controllers 176
Adding Routing 178
Supporting the Relationship Through Views 178
What's Missing? 187
Test Your Knowledge 188
Quiz 188
Answers 188
10 Managing Databases with Migrations 191
What Migrations Offer You 192
Migration Basics 193
Migration Files 193
Running Migrations Forward and Backward 195
Inside Migrations 197
Working with Tables 197
Data Types 198
Working with Columns 201
Indexes 202
Other Opportunities 204
Test Your Knowledge 206
Quiz 206
Answers 207
11 Debugging 209
Creating Your Own Debugging Messages 209
Raising Exceptions 212
Logging 213
Working with Rails from the Console 214
Debug and Debugger 219
Test Your Knowledge 223
Quiz 223
Answers 223
12 Testing 225
Test Mode 225
Testing the Students App 226
Setting Up a Test Database with Fixtures 227
Model Testing 231
Controller Testing 238
Calling Controllers 241
Testing Responses 241
Dealing with Nested Resources 243
Integration Testing 245
Beyond the Basics 248
Test Your Knowledge 249
Quiz 249
Answers 250
13 Sessions and Cookies 251
Getting Into and Out of Cookies 251
Storing Data Between Sessions 257
Test Your Knowledge 262
Quiz 262
Answers 262
14 Users and Authentication 263
The Sign-up Process 264
Wiring Authentication to Your Application 271
Classifying Users 273
More Options 283
Test Your Knowledge 284
Quiz 284
Answers 284
15 Routing 285
Creating Routes to Interpret URIs 286
Specifying Routes 286
Globbing 288
Regular Expressions and Routing 289
A Domain Default with root 289
Mapping Resources 290
Nesting Resources 291
Route Order and Priority 292
Checking the Map 292
Generating URIs from Views and Controllers 293
Pointing url_for in the Right Direction 293
Adding Options 294
Infinite Possibilities 295
Test Your Knowledge 296
Quiz 296
Answers 296
16 From CSS to Sass 297
Getting Started 297
Sassy Style 298
Variables 298
Mixins 300
Nesting 302
Making Everything Work Together 303
Becoming Sassier 306
Test Your Knowledge 307
Quiz 307
Answers 307
17 Managing Assets and Bundles 309
The Junk Drawer 309
Sprockets 310
Dissecting the Pipeline 310
Putting It All Together 311
Bundler 315
Test Your Knowledge 318
Quiz 318
Answers 319
18 Sending Code to the Browser: JavaScript and CoffeeScript 321
Sending JavaScript to the Browser 322
Simplifying with CoffeeScript 325
Have Some Sugar with Your CoffeeScript 325
Converting to CoffeeScript 327
Test Your Knowledge 329
Quiz 329
Answers 329
19 Mail in Rails 331
Sending Mail Messages 331
Previewing Mail 337
Receiving Mail 338
Setup 338
Processing Messages 339
Test Your Knowledge 342
Quiz 342
Answers 342
20 Pushing Further into Rails 343
Changing to Production Mode 343
Deploying Is Much More Than Programming 344
Joining the Rails Ecosystem 347
Keep Up with Rails 347
Ruby 348
Working with and Around Rails 348
Keep Exploring 349
A An Incredibly Brief Introduction to Ruby 351
B An Incredibly Brief Introduction to Relational Databases 373
C An Incredibly Brief Guide to Regular Expressions 383
D Glossary 393
Index 413