The Ruby on Rails Tutorial

1,243 231 32MB

English Pages 915 Year 2020

Report DMCA / Copyright

DOWNLOAD FILE

The Ruby on Rails Tutorial

Table of contents :
From zero to deploy......Page 23
Up and running......Page 29
Development environment......Page 30
Installing Rails......Page 38
The first application......Page 39
Bundler......Page 42
rails server......Page 49
Model-View-Controller (MVC)......Page 56
Hello, world!......Page 57
Version control with Git......Page 61
Installation and setup......Page 64
What good does Git do you?......Page 68
GitHub......Page 69
Branch, edit, commit, merge......Page 74
Heroku setup and deployment......Page 82
Heroku commands......Page 88
What we learned in this chapter......Page 89
Conventions used in this book......Page 90
A toy app......Page 93
Planning the application......Page 94
A toy model for users......Page 98
A toy model for microposts......Page 99
The Users resource......Page 100
A user tour......Page 103
MVC in action......Page 111
The Microposts resource......Page 119
A micropost microtour......Page 120
Putting the micro in microposts......Page 126
A user has_many microposts......Page 128
Inheritance hierarchies......Page 131
Deploying the toy app......Page 137
Conclusion......Page 139
What we learned in this chapter......Page 142
Mostly static pages......Page 143
Sample app setup......Page 144
Static pages......Page 151
Generated static pages......Page 153
Custom static pages......Page 162
Getting started with testing......Page 163
Our first test......Page 168
Red......Page 170
Green......Page 171
Refactor......Page 174
Slightly dynamic pages......Page 176
Testing titles (Red)......Page 177
Adding page titles (Green)......Page 178
Layouts and embedded Ruby (Refactor)......Page 183
Setting the root route......Page 190
Conclusion......Page 193
Advanced testing setup......Page 195
Automated tests with Guard......Page 196
Motivation......Page 201
Built-in helpers......Page 202
Custom helpers......Page 203
Strings and methods......Page 207
Strings......Page 209
Objects and message passing......Page 213
Method definitions......Page 217
Back to the title helper......Page 219
Arrays and ranges......Page 220
Blocks......Page 225
Hashes and symbols......Page 229
CSS revisited......Page 234
Ruby classes......Page 236
Constructors......Page 237
Class inheritance......Page 238
Modifying built-in classes......Page 243
A controller class......Page 245
A user class......Page 248
Conclusion......Page 251
What we learned in this chapter......Page 252
Filling in the layout......Page 253
Adding some structure......Page 254
Site navigation......Page 256
Bootstrap and custom CSS......Page 266
Partials......Page 275
Sass and the asset pipeline......Page 281
The asset pipeline......Page 282
Syntactically awesome stylesheets......Page 285
Layout links......Page 292
Contact page......Page 293
Rails routes......Page 295
Using named routes......Page 299
Layout link tests......Page 300
Users controller......Page 306
Signup URL......Page 308
Conclusion......Page 311
What we learned in this chapter......Page 313
Modeling users......Page 315
User model......Page 316
Database migrations......Page 318
The model file......Page 325
Creating user objects......Page 326
Finding user objects......Page 330
Updating user objects......Page 332
User validations......Page 334
A validity test......Page 335
Validating presence......Page 338
Length validation......Page 342
Format validation......Page 344
Uniqueness validation......Page 351
A hashed password......Page 361
User has secure password......Page 364
Minimum password standards......Page 366
Creating and authenticating a user......Page 369
Conclusion......Page 372
What we learned in this chapter......Page 373
Sign up......Page 375
Debug and Rails environments......Page 376
A Users resource......Page 384
Debugger......Page 390
A Gravatar image and a sidebar......Page 394
Signup form......Page 402
Using form_with......Page 404
Signup form HTML......Page 407
A working form......Page 412
Strong parameters......Page 417
Signup error messages......Page 420
A test for invalid submission......Page 426
Successful signups......Page 429
The finished signup form......Page 431
The flash......Page 433
The first signup......Page 437
A test for valid submission......Page 441
SSL in production......Page 444
Production webserver......Page 446
Production database configuration......Page 447
Production deployment......Page 448
What we learned in this chapter......Page 451
Basic login......Page 453
Sessions......Page 454
Sessions controller......Page 455
Login form......Page 458
Finding and authenticating a user......Page 463
Rendering with a flash message......Page 468
A flash test......Page 471
Logging in......Page 473
The log_in method......Page 474
Current user......Page 477
Changing the layout links......Page 482
Testing layout changes......Page 499
Login upon signup......Page 506
Logging out......Page 509
Conclusion......Page 512
What we learned in this chapter......Page 513
Remember me......Page 515
Remember token and digest......Page 516
Login with remembering......Page 523
Forgetting users......Page 533
Two subtle bugs......Page 536
``Remember me'' checkbox......Page 541
Remember tests......Page 548
Testing the ``remember me'' box......Page 549
Testing the remember branch......Page 555
What we learned in this chapter......Page 559
Updating users......Page 563
Edit form......Page 564
Unsuccessful edits......Page 572
Testing unsuccessful edits......Page 575
Successful edits (with TDD)......Page 576
Authorization......Page 580
Requiring logged-in users......Page 582
Requiring the right user......Page 588
Friendly forwarding......Page 594
Users index......Page 599
Sample users......Page 606
Pagination......Page 609
Users index test......Page 614
Partial refactoring......Page 616
Deleting users......Page 618
Administrative users......Page 619
The destroy action......Page 624
User destroy tests......Page 627
Conclusion......Page 631
What we learned in this chapter......Page 633
Account activation......Page 635
Account activations controller......Page 637
Account activation data model......Page 639
Mailer templates......Page 646
Email previews......Page 652
Email tests......Page 654
Updating the Users create action......Page 659
Generalizing the authenticated? method......Page 664
Activation edit action......Page 670
Activation test and refactoring......Page 673
Email in production......Page 681
What we learned in this chapter......Page 685
Password reset......Page 687
Password resets resource......Page 691
Password resets controller......Page 692
New password resets......Page 695
Password reset create action......Page 698
Password reset mailer and templates......Page 703
Email tests......Page 710
Resetting the password......Page 711
Reset edit action......Page 712
Updating the reset......Page 716
Password reset test......Page 722
Email in production (take two)......Page 728
Conclusion......Page 730
Proof of expiration comparison......Page 732
A Micropost model......Page 735
The basic model......Page 736
Micropost validations......Page 739
User/Micropost associations......Page 742
Micropost refinements......Page 747
Rendering microposts......Page 753
Sample microposts......Page 759
Profile micropost tests......Page 766
Manipulating microposts......Page 769
Micropost access control......Page 770
Creating microposts......Page 774
A proto-feed......Page 784
Destroying microposts......Page 795
Micropost tests......Page 802
Basic image upload......Page 806
Image validation......Page 814
Image resizing......Page 821
Image upload in production......Page 826
Conclusion......Page 834
What we learned in this chapter......Page 837
Following users......Page 839
A problem with the data model (and a solution)......Page 840
User/relationship associations......Page 850
Relationship validations......Page 853
Followed users......Page 855
Followers......Page 859
Sample following data......Page 862
Stats and a follow form......Page 864
Following and followers pages......Page 876
A working follow button the standard way......Page 887
A working follow button with Ajax......Page 892
Following tests......Page 897
The status feed......Page 899
Motivation and strategy......Page 901
A first feed implementation......Page 903
Subselects......Page 907
Guide to further resources......Page 913
What we learned in this chapter......Page 915

Polecaj historie