-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy patha-demo-app.html
1075 lines (659 loc) · 118 KB
/
a-demo-app.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html>
<head>
<title>a-demo-app</title>
<link rel="stylesheet" href="pygments.css" type="text/css" />
<link rel="stylesheet" href="polytexnic.css" type="text/css" />
</head>
<body>
<div id="book">
<h1 class="title">Ruby on Rails Tutorial </h1>
<h1 class="subtitle"> Learn Web Development with Rails</h1>
<h2 class="author">Michael Hartl</h2>
<h2 class="contents">Contents</h2>
<div id="table_of_contents"><ol><li class="chapter"><a href="beginning.html#top"><span class="number">Chapter 1</span> From zero to deploy</a></li><li><ol><li class="section"><a href="beginning.html#sec-introduction"><span class="number">1.1</span> Introduction</a></li><li><ol><li class="subsection"><a href="beginning.html#sec-comments_for_various_readers"><span class="number">1.1.1</span> Comments for various readers</a></li><li class="subsection"><a href="beginning.html#sec-1_1_2"><span class="number">1.1.2</span> “Scaling” Rails</a></li><li class="subsection"><a href="beginning.html#sec-conventions"><span class="number">1.1.3</span> Conventions in this book</a></li></ol></li><li class="section"><a href="beginning.html#sec-up_and_running"><span class="number">1.2</span> Up and running</a></li><li><ol><li class="subsection"><a href="beginning.html#sec-development_tools"><span class="number">1.2.1</span> Development environments</a></li><li><ol><li class="subsubsection"><a href="beginning.html#sec-1_2_1_1">IDEs</a></li><li class="subsubsection"><a href="beginning.html#sec-1_2_1_2">Text editors and command lines</a></li><li class="subsubsection"><a href="beginning.html#sec-1_2_1_3">Browsers</a></li><li class="subsubsection"><a href="beginning.html#sec-1_2_1_4">A note about tools</a></li></ol></li><li class="subsection"><a href="beginning.html#sec-rubygems"><span class="number">1.2.2</span> Ruby, RubyGems, Rails, and Git</a></li><li><ol><li class="subsubsection"><a href="beginning.html#sec-rails_installer_windows">Rails Installer (Windows)</a></li><li class="subsubsection"><a href="beginning.html#sec-install_git">Install Git</a></li><li class="subsubsection"><a href="beginning.html#sec-install_ruby">Install Ruby</a></li><li class="subsubsection"><a href="beginning.html#sec-install_rubygems">Install RubyGems</a></li><li class="subsubsection"><a href="beginning.html#sec-install_rails">Install Rails</a></li></ol></li><li class="subsection"><a href="beginning.html#sec-the_first_application"><span class="number">1.2.3</span> The first application</a></li><li class="subsection"><a href="beginning.html#sec-bundler"><span class="number">1.2.4</span> Bundler</a></li><li class="subsection"><a href="beginning.html#sec-rails_server"><span class="number">1.2.5</span> <tt>rails server</tt></a></li><li class="subsection"><a href="beginning.html#sec-mvc"><span class="number">1.2.6</span> Model-view-controller (MVC)</a></li></ol></li><li class="section"><a href="beginning.html#sec-version_control"><span class="number">1.3</span> Version control with Git</a></li><li><ol><li class="subsection"><a href="beginning.html#sec-git_setup"><span class="number">1.3.1</span> Installation and setup</a></li><li><ol><li class="subsubsection"><a href="beginning.html#sec-1_3_1_1">First-time system setup</a></li><li class="subsubsection"><a href="beginning.html#sec-1_3_1_2">First-time repository setup</a></li></ol></li><li class="subsection"><a href="beginning.html#sec-adding_and_committing"><span class="number">1.3.2</span> Adding and committing</a></li><li class="subsection"><a href="beginning.html#sec-1_3_3"><span class="number">1.3.3</span> What good does Git do you?</a></li><li class="subsection"><a href="beginning.html#sec-github"><span class="number">1.3.4</span> GitHub</a></li><li class="subsection"><a href="beginning.html#sec-git_commands"><span class="number">1.3.5</span> Branch, edit, commit, merge</a></li><li><ol><li class="subsubsection"><a href="beginning.html#sec-git_branch">Branch</a></li><li class="subsubsection"><a href="beginning.html#sec-git_edit">Edit</a></li><li class="subsubsection"><a href="beginning.html#sec-git_commit">Commit</a></li><li class="subsubsection"><a href="beginning.html#sec-git_merge">Merge</a></li><li class="subsubsection"><a href="beginning.html#sec-git_push">Push</a></li></ol></li></ol></li><li class="section"><a href="beginning.html#sec-deploying"><span class="number">1.4</span> Deploying</a></li><li><ol><li class="subsection"><a href="beginning.html#sec-heroku_setup"><span class="number">1.4.1</span> Heroku setup</a></li><li class="subsection"><a href="beginning.html#sec-heroku_step_one"><span class="number">1.4.2</span> Heroku deployment, step one</a></li><li class="subsection"><a href="beginning.html#sec-1_4_3"><span class="number">1.4.3</span> Heroku deployment, step two</a></li><li class="subsection"><a href="beginning.html#sec-heroku_commands"><span class="number">1.4.4</span> Heroku commands</a></li></ol></li><li class="section"><a href="beginning.html#sec-beginning_conclusion"><span class="number">1.5</span> Conclusion</a></li></ol></li><li class="chapter"><a href="a-demo-app.html#top"><span class="number">Chapter 2</span> A demo app</a></li><li><ol><li class="section"><a href="a-demo-app.html#sec-planning_the_application"><span class="number">2.1</span> Planning the application</a></li><li><ol><li class="subsection"><a href="a-demo-app.html#sec-modeling_demo_users"><span class="number">2.1.1</span> Modeling demo users</a></li><li class="subsection"><a href="a-demo-app.html#sec-modeling_demo_microposts"><span class="number">2.1.2</span> Modeling demo microposts</a></li></ol></li><li class="section"><a href="a-demo-app.html#sec-demo_users_resource"><span class="number">2.2</span> The Users resource</a></li><li><ol><li class="subsection"><a href="a-demo-app.html#sec-a_user_tour"><span class="number">2.2.1</span> A user tour</a></li><li class="subsection"><a href="a-demo-app.html#sec-mvc_in_action"><span class="number">2.2.2</span> MVC in action</a></li><li class="subsection"><a href="a-demo-app.html#sec-weaknesses_of_this_users_resource"><span class="number">2.2.3</span> Weaknesses of this Users resource</a></li></ol></li><li class="section"><a href="a-demo-app.html#sec-microposts_resource"><span class="number">2.3</span> The Microposts resource</a></li><li><ol><li class="subsection"><a href="a-demo-app.html#sec-a_micropost_microtour"><span class="number">2.3.1</span> A micropost microtour</a></li><li class="subsection"><a href="a-demo-app.html#sec-putting_the_micro_in_microposts"><span class="number">2.3.2</span> Putting the <em>micro</em> in microposts</a></li><li class="subsection"><a href="a-demo-app.html#sec-demo_user_has_many_microposts"><span class="number">2.3.3</span> A user <tt>has_many</tt> microposts</a></li><li class="subsection"><a href="a-demo-app.html#sec-inheritance_hierarchies"><span class="number">2.3.4</span> Inheritance hierarchies</a></li><li class="subsection"><a href="a-demo-app.html#sec-deploying_the_demo_app"><span class="number">2.3.5</span> Deploying the demo app</a></li></ol></li><li class="section"><a href="a-demo-app.html#sec-2_4"><span class="number">2.4</span> Conclusion</a></li></ol></li><li class="chapter"><a href="static-pages.html#top"><span class="number">Chapter 3</span> Mostly static pages</a></li><li><ol><li class="section"><a href="static-pages.html#sec-static_pages"><span class="number">3.1</span> Static pages</a></li><li><ol><li class="subsection"><a href="static-pages.html#sec-truly_static_pages"><span class="number">3.1.1</span> Truly static pages</a></li><li class="subsection"><a href="static-pages.html#sec-static_pages_with_rails"><span class="number">3.1.2</span> Static pages with Rails</a></li></ol></li><li class="section"><a href="static-pages.html#sec-first_tests"><span class="number">3.2</span> Our first tests</a></li><li><ol><li class="subsection"><a href="static-pages.html#sec-TDD"><span class="number">3.2.1</span> Test-driven development</a></li><li class="subsection"><a href="static-pages.html#sec-adding_a_page"><span class="number">3.2.2</span> Adding a page</a></li><li><ol><li class="subsubsection"><a href="static-pages.html#sec-red">Red</a></li><li class="subsubsection"><a href="static-pages.html#sec-green">Green</a></li><li class="subsubsection"><a href="static-pages.html#sec-refactor">Refactor</a></li></ol></li></ol></li><li class="section"><a href="static-pages.html#sec-slightly_dynamic_pages"><span class="number">3.3</span> Slightly dynamic pages</a></li><li><ol><li class="subsection"><a href="static-pages.html#sec-testing_a_title_change"><span class="number">3.3.1</span> Testing a title change</a></li><li class="subsection"><a href="static-pages.html#sec-passing_title_tests"><span class="number">3.3.2</span> Passing title tests</a></li><li class="subsection"><a href="static-pages.html#sec-embedded_ruby"><span class="number">3.3.3</span> Embedded Ruby</a></li><li class="subsection"><a href="static-pages.html#sec-layouts"><span class="number">3.3.4</span> Eliminating duplication with layouts</a></li></ol></li><li class="section"><a href="static-pages.html#sec-static_pages_conclusion"><span class="number">3.4</span> Conclusion</a></li><li class="section"><a href="static-pages.html#sec-static_pages_exercises"><span class="number">3.5</span> Exercises</a></li><li class="section"><a href="static-pages.html#sec-advanced_setup"><span class="number">3.6</span> Advanced setup</a></li><li><ol><li class="subsection"><a href="static-pages.html#sec-eliminating_bundle_exec"><span class="number">3.6.1</span> Eliminating <tt>bundle exec</tt></a></li><li><ol><li class="subsubsection"><a href="static-pages.html#sec-rvm_bundler_integration">RVM Bundler integration</a></li><li class="subsubsection"><a href="static-pages.html#sec-binstubs">binstubs</a></li></ol></li><li class="subsection"><a href="static-pages.html#sec-guard"><span class="number">3.6.2</span> Automated tests with Guard</a></li><li class="subsection"><a href="static-pages.html#sec-spork"><span class="number">3.6.3</span> Speeding up tests with Spork</a></li><li><ol><li class="subsubsection"><a href="static-pages.html#sec-spork_and_guard">Guard with Spork</a></li></ol></li><li class="subsection"><a href="static-pages.html#sec-tests_inside_sublime_text"><span class="number">3.6.4</span> Tests inside Sublime Text</a></li></ol></li></ol></li><li class="chapter"><a href="rails-flavored-ruby.html#top"><span class="number">Chapter 4</span> Rails-flavored Ruby</a></li><li><ol><li class="section"><a href="rails-flavored-ruby.html#sec-motivation"><span class="number">4.1</span> Motivation</a></li><li class="section"><a href="rails-flavored-ruby.html#sec-strings_and_methods"><span class="number">4.2</span> Strings and methods</a></li><li><ol><li class="subsection"><a href="rails-flavored-ruby.html#sec-comments"><span class="number">4.2.1</span> Comments</a></li><li class="subsection"><a href="rails-flavored-ruby.html#sec-strings"><span class="number">4.2.2</span> Strings</a></li><li><ol><li class="subsubsection"><a href="rails-flavored-ruby.html#sec-printing">Printing</a></li><li class="subsubsection"><a href="rails-flavored-ruby.html#sec-single_quoted_strings">Single-quoted strings</a></li></ol></li><li class="subsection"><a href="rails-flavored-ruby.html#sec-objects_and_message_passing"><span class="number">4.2.3</span> Objects and message passing</a></li><li class="subsection"><a href="rails-flavored-ruby.html#sec-method_definitions"><span class="number">4.2.4</span> Method definitions</a></li><li class="subsection"><a href="rails-flavored-ruby.html#sec-back_to_the_title_helper"><span class="number">4.2.5</span> Back to the title helper</a></li></ol></li><li class="section"><a href="rails-flavored-ruby.html#sec-other_data_structures"><span class="number">4.3</span> Other data structures</a></li><li><ol><li class="subsection"><a href="rails-flavored-ruby.html#sec-arrays_and_ranges"><span class="number">4.3.1</span> Arrays and ranges</a></li><li class="subsection"><a href="rails-flavored-ruby.html#sec-blocks"><span class="number">4.3.2</span> Blocks</a></li><li class="subsection"><a href="rails-flavored-ruby.html#sec-hashes_and_symbols"><span class="number">4.3.3</span> Hashes and symbols</a></li><li class="subsection"><a href="rails-flavored-ruby.html#sec-css_revisited"><span class="number">4.3.4</span> CSS revisited</a></li></ol></li><li class="section"><a href="rails-flavored-ruby.html#sec-ruby_classes"><span class="number">4.4</span> Ruby classes</a></li><li><ol><li class="subsection"><a href="rails-flavored-ruby.html#sec-constructors"><span class="number">4.4.1</span> Constructors</a></li><li class="subsection"><a href="rails-flavored-ruby.html#sec-a_class_of_our_own"><span class="number">4.4.2</span> Class inheritance</a></li><li class="subsection"><a href="rails-flavored-ruby.html#sec-modifying_built_in_classes"><span class="number">4.4.3</span> Modifying built-in classes</a></li><li class="subsection"><a href="rails-flavored-ruby.html#sec-a_controller_class"><span class="number">4.4.4</span> A controller class</a></li><li class="subsection"><a href="rails-flavored-ruby.html#sec-a_user_class"><span class="number">4.4.5</span> A user class</a></li></ol></li><li class="section"><a href="rails-flavored-ruby.html#sec-conclusion"><span class="number">4.5</span> Conclusion</a></li><li class="section"><a href="rails-flavored-ruby.html#sec-exercises"><span class="number">4.6</span> Exercises</a></li></ol></li><li class="chapter"><a href="filling-in-the-layout.html#top"><span class="number">Chapter 5</span> Filling in the layout</a></li><li><ol><li class="section"><a href="filling-in-the-layout.html#sec-structure"><span class="number">5.1</span> Adding some structure</a></li><li><ol><li class="subsection"><a href="filling-in-the-layout.html#sec-adding_to_the_layout"><span class="number">5.1.1</span> Site navigation</a></li><li class="subsection"><a href="filling-in-the-layout.html#sec-custom_css"><span class="number">5.1.2</span> Bootstrap and custom CSS</a></li><li class="subsection"><a href="filling-in-the-layout.html#sec-partials"><span class="number">5.1.3</span> Partials</a></li></ol></li><li class="section"><a href="filling-in-the-layout.html#sec-sass_and_the_asset_pipeline"><span class="number">5.2</span> Sass and the asset pipeline</a></li><li><ol><li class="subsection"><a href="filling-in-the-layout.html#sec-the_asset_pipeline"><span class="number">5.2.1</span> The asset pipeline</a></li><li><ol><li class="subsubsection"><a href="filling-in-the-layout.html#sec-5_2_1_1">Asset directories</a></li><li class="subsubsection"><a href="filling-in-the-layout.html#sec-5_2_1_2">Manifest files</a></li><li class="subsubsection"><a href="filling-in-the-layout.html#sec-5_2_1_3">Preprocessor engines</a></li><li class="subsubsection"><a href="filling-in-the-layout.html#sec-5_2_1_4">Efficiency in production</a></li></ol></li><li class="subsection"><a href="filling-in-the-layout.html#sec-sass"><span class="number">5.2.2</span> Syntactically awesome stylesheets</a></li><li><ol><li class="subsubsection"><a href="filling-in-the-layout.html#sec-5_2_2_1">Nesting</a></li><li class="subsubsection"><a href="filling-in-the-layout.html#sec-5_2_2_2">Variables</a></li></ol></li></ol></li><li class="section"><a href="filling-in-the-layout.html#sec-layout_links"><span class="number">5.3</span> Layout links</a></li><li><ol><li class="subsection"><a href="filling-in-the-layout.html#sec-route_tests"><span class="number">5.3.1</span> Route tests</a></li><li class="subsection"><a href="filling-in-the-layout.html#sec-rails_routes"><span class="number">5.3.2</span> Rails routes</a></li><li class="subsection"><a href="filling-in-the-layout.html#sec-named_routes"><span class="number">5.3.3</span> Named routes</a></li><li class="subsection"><a href="filling-in-the-layout.html#sec-pretty_rspec"><span class="number">5.3.4</span> Pretty RSpec</a></li></ol></li><li class="section"><a href="filling-in-the-layout.html#sec-user_signup"><span class="number">5.4</span> User signup: A first step</a></li><li><ol><li class="subsection"><a href="filling-in-the-layout.html#sec-users_controller"><span class="number">5.4.1</span> Users controller</a></li><li class="subsection"><a href="filling-in-the-layout.html#sec-signup_url"><span class="number">5.4.2</span> Signup URI</a></li></ol></li><li class="section"><a href="filling-in-the-layout.html#sec-layout_conclusion"><span class="number">5.5</span> Conclusion</a></li><li class="section"><a href="filling-in-the-layout.html#sec-layout_exercises"><span class="number">5.6</span> Exercises</a></li></ol></li><li class="chapter"><a href="modeling-users.html#top"><span class="number">Chapter 6</span> Modeling users</a></li><li><ol><li class="section"><a href="modeling-users.html#sec-user_model"><span class="number">6.1</span> User model</a></li><li><ol><li class="subsection"><a href="modeling-users.html#sec-database_migrations"><span class="number">6.1.1</span> Database migrations</a></li><li class="subsection"><a href="modeling-users.html#sec-the_model_file"><span class="number">6.1.2</span> The model file</a></li><li><ol><li class="subsubsection"><a href="modeling-users.html#sec-model_annotation">Model annotation</a></li><li class="subsubsection"><a href="modeling-users.html#sec-accessible_attributes">Accessible attributes</a></li></ol></li><li class="subsection"><a href="modeling-users.html#sec-creating_user_objects"><span class="number">6.1.3</span> Creating user objects</a></li><li class="subsection"><a href="modeling-users.html#sec-finding_user_objects"><span class="number">6.1.4</span> Finding user objects</a></li><li class="subsection"><a href="modeling-users.html#sec-updating_user_objects"><span class="number">6.1.5</span> Updating user objects</a></li></ol></li><li class="section"><a href="modeling-users.html#sec-user_validations"><span class="number">6.2</span> User validations</a></li><li><ol><li class="subsection"><a href="modeling-users.html#sec-initial_user_tests"><span class="number">6.2.1</span> Initial user tests</a></li><li class="subsection"><a href="modeling-users.html#sec-presence_validation"><span class="number">6.2.2</span> Validating presence</a></li><li class="subsection"><a href="modeling-users.html#sec-length_validation"><span class="number">6.2.3</span> Length validation</a></li><li class="subsection"><a href="modeling-users.html#sec-format_validation"><span class="number">6.2.4</span> Format validation</a></li><li class="subsection"><a href="modeling-users.html#sec-uniqueness_validation"><span class="number">6.2.5</span> Uniqueness validation</a></li><li><ol><li class="subsubsection"><a href="modeling-users.html#sec-the_caveat">The uniqueness caveat</a></li></ol></li></ol></li><li class="section"><a href="modeling-users.html#sec-adding_a_secure_password"><span class="number">6.3</span> Adding a secure password</a></li><li><ol><li class="subsection"><a href="modeling-users.html#sec-an_encrypted_password"><span class="number">6.3.1</span> An encrypted password</a></li><li class="subsection"><a href="modeling-users.html#sec-password_and_confirmation"><span class="number">6.3.2</span> Password and confirmation</a></li><li class="subsection"><a href="modeling-users.html#sec-user_authentication"><span class="number">6.3.3</span> User authentication</a></li><li class="subsection"><a href="modeling-users.html#sec-has_secure_password"><span class="number">6.3.4</span> User has secure password</a></li><li class="subsection"><a href="modeling-users.html#sec-creating_a_user"><span class="number">6.3.5</span> Creating a user</a></li></ol></li><li class="section"><a href="modeling-users.html#sec-6_4"><span class="number">6.4</span> Conclusion</a></li><li class="section"><a href="modeling-users.html#sec-6_5"><span class="number">6.5</span> Exercises</a></li></ol></li><li class="chapter"><a href="sign-up.html#top"><span class="number">Chapter 7</span> Sign up</a></li><li><ol><li class="section"><a href="sign-up.html#sec-showing_users"><span class="number">7.1</span> Showing users</a></li><li><ol><li class="subsection"><a href="sign-up.html#sec-rails_environments"><span class="number">7.1.1</span> Debug and Rails environments</a></li><li class="subsection"><a href="sign-up.html#sec-a_users_resource"><span class="number">7.1.2</span> A Users resource</a></li><li class="subsection"><a href="sign-up.html#sec-tests_with_factories"><span class="number">7.1.3</span> Testing the user show page (with factories)</a></li><li class="subsection"><a href="sign-up.html#sec-a_gravatar_image"><span class="number">7.1.4</span> A Gravatar image and a sidebar</a></li></ol></li><li class="section"><a href="sign-up.html#sec-signup_form"><span class="number">7.2</span> Signup form</a></li><li><ol><li class="subsection"><a href="sign-up.html#sec-tests_for_user_signup"><span class="number">7.2.1</span> Tests for user signup</a></li><li class="subsection"><a href="sign-up.html#sec-using_form_for"><span class="number">7.2.2</span> Using <tt>form_for</tt></a></li><li class="subsection"><a href="sign-up.html#sec-the_form_html"><span class="number">7.2.3</span> The form HTML</a></li></ol></li><li class="section"><a href="sign-up.html#sec-signup_failure"><span class="number">7.3</span> Signup failure</a></li><li><ol><li class="subsection"><a href="sign-up.html#sec-a_working_form"><span class="number">7.3.1</span> A working form</a></li><li class="subsection"><a href="sign-up.html#sec-signup_error_messages"><span class="number">7.3.2</span> Signup error messages</a></li></ol></li><li class="section"><a href="sign-up.html#sec-signup_success"><span class="number">7.4</span> Signup success</a></li><li><ol><li class="subsection"><a href="sign-up.html#sec-the_finished_signup_form"><span class="number">7.4.1</span> The finished signup form</a></li><li class="subsection"><a href="sign-up.html#sec-the_flash"><span class="number">7.4.2</span> The flash</a></li><li class="subsection"><a href="sign-up.html#sec-the_first_signup"><span class="number">7.4.3</span> The first signup</a></li><li class="subsection"><a href="sign-up.html#sec-deploying_to_production_with_ssl"><span class="number">7.4.4</span> Deploying to production with SSL</a></li></ol></li><li class="section"><a href="sign-up.html#sec-7_5"><span class="number">7.5</span> Conclusion</a></li><li class="section"><a href="sign-up.html#sec-signup_exercises"><span class="number">7.6</span> Exercises</a></li></ol></li><li class="chapter"><a href="sign-in-sign-out.html#top"><span class="number">Chapter 8</span> Sign in, sign out</a></li><li><ol><li class="section"><a href="sign-in-sign-out.html#sec-signin_failure"><span class="number">8.1</span> Sessions and signin failure</a></li><li><ol><li class="subsection"><a href="sign-in-sign-out.html#sec-sessions_controller"><span class="number">8.1.1</span> Sessions controller</a></li><li class="subsection"><a href="sign-in-sign-out.html#sec-signin_tests"><span class="number">8.1.2</span> Signin tests</a></li><li class="subsection"><a href="sign-in-sign-out.html#sec-signin_form"><span class="number">8.1.3</span> Signin form</a></li><li class="subsection"><a href="sign-in-sign-out.html#sec-reviewing_form_submission"><span class="number">8.1.4</span> Reviewing form submission</a></li><li class="subsection"><a href="sign-in-sign-out.html#sec-rendering_with_a_flash_message"><span class="number">8.1.5</span> Rendering with a flash message</a></li></ol></li><li class="section"><a href="sign-in-sign-out.html#sec-signin_success"><span class="number">8.2</span> Signin success</a></li><li><ol><li class="subsection"><a href="sign-in-sign-out.html#sec-remember_me"><span class="number">8.2.1</span> Remember me</a></li><li class="subsection"><a href="sign-in-sign-out.html#sec-a_working_sign_in_method"><span class="number">8.2.2</span> A working <tt>sign_in</tt> method</a></li><li class="subsection"><a href="sign-in-sign-out.html#sec-current_user"><span class="number">8.2.3</span> Current user</a></li><li class="subsection"><a href="sign-in-sign-out.html#sec-changing_the_layout_links"><span class="number">8.2.4</span> Changing the layout links</a></li><li class="subsection"><a href="sign-in-sign-out.html#sec-signin_upon_signup"><span class="number">8.2.5</span> Signin upon signup</a></li><li class="subsection"><a href="sign-in-sign-out.html#sec-signing_out"><span class="number">8.2.6</span> Signing out</a></li></ol></li><li class="section"><a href="sign-in-sign-out.html#sec-cucumber"><span class="number">8.3</span> Introduction to Cucumber (optional)</a></li><li><ol><li class="subsection"><a href="sign-in-sign-out.html#sec-installation_and_setup"><span class="number">8.3.1</span> Installation and setup</a></li><li class="subsection"><a href="sign-in-sign-out.html#sec-features_and_steps"><span class="number">8.3.2</span> Features and steps</a></li><li class="subsection"><a href="sign-in-sign-out.html#sec-rspec_custom_matchers"><span class="number">8.3.3</span> Counterpoint: RSpec custom matchers</a></li></ol></li><li class="section"><a href="sign-in-sign-out.html#sec-8_4"><span class="number">8.4</span> Conclusion</a></li><li class="section"><a href="sign-in-sign-out.html#sec-sign_in_out_exercises"><span class="number">8.5</span> Exercises</a></li></ol></li><li class="chapter"><a href="updating-showing-and-deleting-users.html#top"><span class="number">Chapter 9</span> Updating, showing, and deleting users</a></li><li><ol><li class="section"><a href="updating-showing-and-deleting-users.html#sec-updating_users"><span class="number">9.1</span> Updating users</a></li><li><ol><li class="subsection"><a href="updating-showing-and-deleting-users.html#sec-edit_form"><span class="number">9.1.1</span> Edit form</a></li><li class="subsection"><a href="updating-showing-and-deleting-users.html#sec-unsuccessful_edits"><span class="number">9.1.2</span> Unsuccessful edits</a></li><li class="subsection"><a href="updating-showing-and-deleting-users.html#sec-successful_edits"><span class="number">9.1.3</span> Successful edits</a></li></ol></li><li class="section"><a href="updating-showing-and-deleting-users.html#sec-authorization"><span class="number">9.2</span> Authorization</a></li><li><ol><li class="subsection"><a href="updating-showing-and-deleting-users.html#sec-requiring_signed_in_users"><span class="number">9.2.1</span> Requiring signed-in users</a></li><li class="subsection"><a href="updating-showing-and-deleting-users.html#sec-requiring_the_right_user"><span class="number">9.2.2</span> Requiring the right user</a></li><li class="subsection"><a href="updating-showing-and-deleting-users.html#sec-friendly_forwarding"><span class="number">9.2.3</span> Friendly forwarding</a></li></ol></li><li class="section"><a href="updating-showing-and-deleting-users.html#sec-showing_all_users"><span class="number">9.3</span> Showing all users</a></li><li><ol><li class="subsection"><a href="updating-showing-and-deleting-users.html#sec-user_index"><span class="number">9.3.1</span> User index</a></li><li class="subsection"><a href="updating-showing-and-deleting-users.html#sec-sample_users"><span class="number">9.3.2</span> Sample users</a></li><li class="subsection"><a href="updating-showing-and-deleting-users.html#sec-pagination"><span class="number">9.3.3</span> Pagination</a></li><li class="subsection"><a href="updating-showing-and-deleting-users.html#sec-partial_refactoring"><span class="number">9.3.4</span> Partial refactoring</a></li></ol></li><li class="section"><a href="updating-showing-and-deleting-users.html#sec-destroying_users"><span class="number">9.4</span> Deleting users</a></li><li><ol><li class="subsection"><a href="updating-showing-and-deleting-users.html#sec-administrative_users"><span class="number">9.4.1</span> Administrative users</a></li><li><ol><li class="subsubsection"><a href="updating-showing-and-deleting-users.html#sec-revisiting_attr_accessible">Revisiting <tt>attr_accessible</tt></a></li></ol></li><li class="subsection"><a href="updating-showing-and-deleting-users.html#sec-the_destroy_action"><span class="number">9.4.2</span> The <tt>destroy</tt> action</a></li></ol></li><li class="section"><a href="updating-showing-and-deleting-users.html#sec-updating_and_deleting_users_conclusion"><span class="number">9.5</span> Conclusion</a></li><li class="section"><a href="updating-showing-and-deleting-users.html#sec-updating_deleting_exercises"><span class="number">9.6</span> Exercises</a></li></ol></li><li class="chapter"><a href="user-microposts.html#top"><span class="number">Chapter 10</span> User microposts</a></li><li><ol><li class="section"><a href="user-microposts.html#sec-a_micropost_model"><span class="number">10.1</span> A Micropost model</a></li><li><ol><li class="subsection"><a href="user-microposts.html#sec-the_basic_model"><span class="number">10.1.1</span> The basic model</a></li><li class="subsection"><a href="user-microposts.html#sec-accessible_attribute"><span class="number">10.1.2</span> Accessible attributes and the first validation</a></li><li class="subsection"><a href="user-microposts.html#sec-user_micropost_associations"><span class="number">10.1.3</span> User/Micropost associations</a></li><li class="subsection"><a href="user-microposts.html#sec-ordering_and_dependency"><span class="number">10.1.4</span> Micropost refinements</a></li><li><ol><li class="subsubsection"><a href="user-microposts.html#sec-default_scope">Default scope</a></li><li class="subsubsection"><a href="user-microposts.html#sec-dependent_destroy">Dependent: destroy</a></li></ol></li><li class="subsection"><a href="user-microposts.html#sec-micropost_validations"><span class="number">10.1.5</span> Content validations</a></li></ol></li><li class="section"><a href="user-microposts.html#sec-showing_microposts"><span class="number">10.2</span> Showing microposts</a></li><li><ol><li class="subsection"><a href="user-microposts.html#sec-augmenting_the_user_show_page"><span class="number">10.2.1</span> Augmenting the user show page</a></li><li class="subsection"><a href="user-microposts.html#sec-sample_microposts"><span class="number">10.2.2</span> Sample microposts</a></li></ol></li><li class="section"><a href="user-microposts.html#sec-manipulating_microposts"><span class="number">10.3</span> Manipulating microposts</a></li><li><ol><li class="subsection"><a href="user-microposts.html#sec-access_control"><span class="number">10.3.1</span> Access control</a></li><li class="subsection"><a href="user-microposts.html#sec-creating_microposts"><span class="number">10.3.2</span> Creating microposts</a></li><li class="subsection"><a href="user-microposts.html#sec-a_proto_feed"><span class="number">10.3.3</span> A proto-feed</a></li><li class="subsection"><a href="user-microposts.html#sec-destroying_microposts"><span class="number">10.3.4</span> Destroying microposts</a></li></ol></li><li class="section"><a href="user-microposts.html#sec-10_4"><span class="number">10.4</span> Conclusion</a></li><li class="section"><a href="user-microposts.html#sec-micropost_exercises"><span class="number">10.5</span> Exercises</a></li></ol></li><li class="chapter"><a href="following-users.html#top"><span class="number">Chapter 11</span> Following users</a></li><li><ol><li class="section"><a href="following-users.html#sec-the_relationship_model"><span class="number">11.1</span> The Relationship model</a></li><li><ol><li class="subsection"><a href="following-users.html#sec-a_problem_with_the_data_model"><span class="number">11.1.1</span> A problem with the data model (and a solution)</a></li><li class="subsection"><a href="following-users.html#sec-relationship_user_associations"><span class="number">11.1.2</span> User/relationship associations</a></li><li class="subsection"><a href="following-users.html#sec-relationship_validations"><span class="number">11.1.3</span> Validations</a></li><li class="subsection"><a href="following-users.html#sec-following"><span class="number">11.1.4</span> Followed users</a></li><li class="subsection"><a href="following-users.html#sec-followers"><span class="number">11.1.5</span> Followers</a></li></ol></li><li class="section"><a href="following-users.html#sec-a_web_interface_for_following_and_followers"><span class="number">11.2</span> A web interface for following users</a></li><li><ol><li class="subsection"><a href="following-users.html#sec-sample_following_data"><span class="number">11.2.1</span> Sample following data</a></li><li class="subsection"><a href="following-users.html#sec-stats_and_a_follow_form"><span class="number">11.2.2</span> Stats and a follow form</a></li><li class="subsection"><a href="following-users.html#sec-following_and_followers_pages"><span class="number">11.2.3</span> Following and followers pages</a></li><li class="subsection"><a href="following-users.html#sec-a_working_follow_button_the_standard_way"><span class="number">11.2.4</span> A working follow button the standard way</a></li><li class="subsection"><a href="following-users.html#sec-a_working_follow_button_with_ajax"><span class="number">11.2.5</span> A working follow button with Ajax</a></li></ol></li><li class="section"><a href="following-users.html#sec-the_status_feed"><span class="number">11.3</span> The status feed</a></li><li><ol><li class="subsection"><a href="following-users.html#sec-motivation_and_strategy"><span class="number">11.3.1</span> Motivation and strategy</a></li><li class="subsection"><a href="following-users.html#sec-a_first_feed_implementation"><span class="number">11.3.2</span> A first feed implementation</a></li><li class="subsection"><a href="following-users.html#sec-scopes_subselects_and_a_lambda"><span class="number">11.3.3</span> Subselects</a></li><li class="subsection"><a href="following-users.html#sec-the_new_status_feed"><span class="number">11.3.4</span> The new status feed</a></li></ol></li><li class="section"><a href="following-users.html#sec-following_conclusion"><span class="number">11.4</span> Conclusion</a></li><li><ol><li class="subsection"><a href="following-users.html#sec-extensions_to_the_sample_application"><span class="number">11.4.1</span> Extensions to the sample application</a></li><li><ol><li class="subsubsection"><a href="following-users.html#sec-replies">Replies</a></li><li class="subsubsection"><a href="following-users.html#sec-messaging">Messaging</a></li><li class="subsubsection"><a href="following-users.html#sec-follower_notifications">Follower notifications</a></li><li class="subsubsection"><a href="following-users.html#sec-password_reminders">Password reminders</a></li><li class="subsubsection"><a href="following-users.html#sec-signup_confirmation">Signup confirmation</a></li><li class="subsubsection"><a href="following-users.html#sec-rss_feed">RSS feed</a></li><li class="subsubsection"><a href="following-users.html#sec-rest_api">REST API</a></li><li class="subsubsection"><a href="following-users.html#sec-search">Search</a></li></ol></li><li class="subsection"><a href="following-users.html#sec-guide_to_further_resources"><span class="number">11.4.2</span> Guide to further resources</a></li></ol></li><li class="section"><a href="following-users.html#sec-following_exercises"><span class="number">11.5</span> Exercises</a></li></ol></li><li class="chapter"><a href="supplement.html#top"><span class="number">Chapter 12</span> Rails 4.0 supplement</a></li><li><ol><li class="section"><a href="supplement.html#sec-upgrading_from_rails_3_2_to_4_0"><span class="number">12.1</span> Upgrading from Rails 3.2 to 4.0</a></li><li><ol><li class="subsection"><a href="supplement.html#sec-rails_4_0_setup"><span class="number">12.1.1</span> Rails 4.0 setup</a></li><li class="subsection"><a href="supplement.html#sec-getting_to_green"><span class="number">12.1.2</span> Getting to green</a></li><li class="subsection"><a href="supplement.html#sec-some_specific_issues"><span class="number">12.1.3</span> Some specific issues</a></li><li><ol><li class="subsubsection"><a href="supplement.html#sec-models">Models</a></li><li class="subsubsection"><a href="supplement.html#sec-controllers_and_views">Controllers and views</a></li></ol></li><li class="subsection"><a href="supplement.html#sec-finishing_up"><span class="number">12.1.4</span> Finishing up</a></li><li class="subsection"><a href="supplement.html#sec-additional_resources"><span class="number">12.1.5</span> Additional resources</a></li></ol></li><li class="section"><a href="supplement.html#sec-strong_parameters"><span class="number">12.2</span> Strong parameters</a></li><li class="section"><a href="supplement.html#sec-security_updates"><span class="number">12.3</span> Security updates</a></li><li><ol><li class="subsection"><a href="supplement.html#sec-secret_key"><span class="number">12.3.1</span> Secret key</a></li><li class="subsection"><a href="supplement.html#sec-encrypted_remember_tokens"><span class="number">12.3.2</span> Hashed remember tokens</a></li></ol></li></ol></li></ol></div>
<div id="main_content"></div>
<p> <span class="preamble">
<span id="foreword">
<strong> Foreword</strong> <br />
</span>
</span></p>
<p>My former company (CD Baby) was one of the first to loudly switch to Ruby on Rails, and then even more loudly switch back to PHP (Google me to read about the drama). This book by Michael Hartl came so highly recommended that I had to try it, and the <em>Ruby on Rails Tutorial</em> is what I used to switch back to Rails again.</p>
<p>Though I’ve worked my way through many Rails books, this is the one that finally made me “get” it. Everything is done very much “the Rails way”—a way that felt very unnatural to me before, but now after doing this book finally feels natural. This is also the only Rails book that does test-driven development the entire time, an approach highly recommended by the experts but which has never been so clearly demonstrated before. Finally, by including Git, GitHub, and Heroku in the demo examples, the author really gives you a feel for what it’s like to do a real-world project. The tutorial’s code examples are not in isolation.</p>
<p>The linear narrative is such a great format. Personally, I powered through the <em>Rails Tutorial</em> in three long days, doing all the examples and challenges at the end of each chapter. Do it from start to finish, without jumping around, and you’ll get the ultimate benefit.</p>
<p>Enjoy!</p>
<p><a href="http://sivers.org/">Derek Sivers</a> (<a href="http://sivers.org/">sivers.org</a>) <br />
<em>Founder, CD Baby</em> <br /></p>
<p> <span class="preamble">
<strong> Acknowledgments</strong> <br />
</span></p>
<p>The <em>Ruby on Rails Tutorial</em> owes a lot to my previous Rails book, <em>RailsSpace</em>, and hence to my coauthor <a href="http://aure.com/">Aurelius Prochazka</a>. I’d like to thank Aure both for the work he did on that book and for his support of this one. I’d also like to thank Debra Williams Cauley, my editor on both <em>RailsSpace</em> and the <em>Ruby on Rails Tutorial</em>; as long as she keeps taking me to baseball games, I’ll keep writing books for her.</p>
<p>I’d like to acknowledge a long list of Rubyists who have taught and inspired me over the years: David Heinemeier Hansson, Yehuda Katz, Carl Lerche, Jeremy Kemper, Xavier Noria, Ryan Bates, Geoffrey Grosenbach, Peter Cooper, Matt Aimonetti, Gregg Pollack, Wayne E. Seguin, Amy Hoy, Dave Chelimsky, Pat Maddox, Tom Preston-Werner, Chris Wanstrath, Chad Fowler, Josh Susser, Obie Fernandez, Ian McFarland, Steven Bristol, Pratik Naik, Sarah Mei, Sarah Allen, Wolfram Arnold, Alex Chaffee, Giles Bowkett, Evan Dorn, Long Nguyen, James Lindenbaum, Adam Wiggins, Tikhon Bernstam, Ron Evans, Wyatt Greene, Miles Forrest, the good people at Pivotal Labs, the Heroku gang, the thoughtbot guys, and the GitHub crew. Finally, many, many readers—far too many to list—have contributed a huge number of bug reports and suggestions during the writing of this book, and I gratefully acknowledge their help in making it as good as it can be. <br /></p>
<p> <span class="preamble">
<span id="author">
<strong> About the author</strong> <br />
</span>
</span></p>
<p><a href="http://michaelhartl.com/">Michael Hartl</a> is the author of the <a href="http://ruby.railstutorial.org/"><em>Ruby on Rails Tutorial</em></a>, the leading introduction to web development with <a href="http://rubyonrails.org/">Ruby on Rails</a>. His prior experience includes writing and developing <em>RailsSpace</em>, an extremely obsolete Rails tutorial book, and developing Insoshi, a once-popular and now-obsolete social networking platform in Ruby on Rails. In 2011, Michael received a <a href="http://rubyheroes.com/heroes">Ruby Hero Award</a> for his contributions to the Ruby community. He is a graduate of <a href="http://college.harvard.edu/">Harvard College</a>, has a <a href="http://resolver.caltech.edu/CaltechETD:etd-05222003-161626">Ph.D. in Physics</a> from <a href="http://www.caltech.edu/">Caltech</a>, and is an alumnus of the <a href="http://ycombinator.com/">Y Combinator</a> entrepreneur program. <br /></p>
<p> <span id="license" class="preamble">
<strong> Copyright and license</strong> <br />
</span></p>
<p><em>Ruby on Rails Tutorial: Learn Web Development with Rails</em>. Copyright © 2012 by Michael Hartl. All source code in the <em>Ruby on Rails Tutorial</em> is available jointly under the <a href="http://www.opensource.org/licenses/mit-license.php">MIT License</a> and the <a href="http://people.freebsd.org/~phk/">Beerware License</a>.</p>
<div class="code"><div class="highlight"><pre>The MIT License
Copyright (c) 2012 Michael Hartl
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
</pre></div>
</div>
<div class="code"><div class="highlight"><pre>/*
* ----------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* Michael Hartl wrote this code. As long as you retain this notice you
* can do whatever you want with this stuff. If we meet some day, and you think
* this stuff is worth it, you can buy me a beer in return.
* ----------------------------------------------------------------------------
*/
</pre></div>
</div>
<div id="top"></div>
<h1 class="chapter"><a id="sec-2" href="a-demo-app.html#top" class="heading"><span class="number">Chapter 2</span> A demo app</a></h1>
<p>In this chapter, we’ll develop a simple demonstration application to show off some of the power of Rails. The purpose is to get a high-level overview of Ruby on Rails programming (and web development in general) by rapidly generating an application using <em>scaffold generators</em>. As discussed in <a class="ref" href="beginning.html#sidebar-scaffolding">Box 1.2</a>, the rest of the book will take the opposite approach, developing a full application incrementally and explaining each new concept as it arises, but for a quick overview (and some instant gratification) there is no substitute for scaffolding. The resulting demo app will allow us to interact with it through its URIs, giving us insight into the structure of a Rails application, including a first example of the <em>REST architecture</em> favored by Rails.</p>
<p>As with the forthcoming sample application, the demo app will consist of <em>users</em> and their associated <em>microposts</em> (thus constituting a minimalist Twitter-style app). The functionality will be utterly under-developed, and many of the steps will seem like magic, but worry not: the full sample app will develop a similar application from the ground up starting in <a class="ref" href="static-pages.html#top">Chapter 3</a>, and I will provide plentiful forward-references to later material. In the mean time, have patience and a little faith—the whole point of this tutorial is to take you <em>beyond</em> this superficial, scaffold-driven approach to achieve a deeper understanding of Rails.</p>
<div class="label" id="sec-planning_the_application"></div>
<h2><a id="sec-2_1" href="a-demo-app.html#sec-planning_the_application" class="heading"><span class="number">2.1</span> Planning the application</a></h2>
<p>In this section, we’ll outline our plans for the demo application. As in <a class="ref" href="beginning.html#sec-the_first_application">Section 1.2.3</a>, we’ll start by generating the application skeleton using the <code>rails</code> command:</p>
<div class="code"><div class="highlight"><pre><span class="gp">$</span> <span class="nb">cd</span> ~/rails_projects
<span class="gp">$</span> rails new demo_app
<span class="gp">$</span> <span class="nb">cd </span>demo_app
</pre></div>
</div>
<p>Next, we’ll use a text editor to update the <code>Gemfile</code> needed by Bundler with the contents of <a class="ref" href="a-demo-app.html#code-demo_gemfile_sqlite_version_redux">Listing 2.1</a>.</p>
<div class="label" id="code-demo_gemfile_sqlite_version_redux"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 2.1.</span> <span class="description">A <code>Gemfile</code> for the demo app.</span> </div>
<div class="code"><div class="highlight"><pre><span class="n">source</span> <span class="s1">'https://rubygems.org'</span>
<span class="n">gem</span> <span class="s1">'rails'</span><span class="p">,</span> <span class="s1">'3.2.14'</span>
<span class="n">group</span> <span class="ss">:development</span> <span class="k">do</span>
<span class="n">gem</span> <span class="s1">'sqlite3'</span><span class="p">,</span> <span class="s1">'1.3.5'</span>
<span class="k">end</span>
<span class="c1"># Gems used only for assets and not required</span>
<span class="c1"># in production environments by default.</span>
<span class="n">group</span> <span class="ss">:assets</span> <span class="k">do</span>
<span class="n">gem</span> <span class="s1">'sass-rails'</span><span class="p">,</span> <span class="s1">'3.2.5'</span>
<span class="n">gem</span> <span class="s1">'coffee-rails'</span><span class="p">,</span> <span class="s1">'3.2.2'</span>
<span class="n">gem</span> <span class="s1">'uglifier'</span><span class="p">,</span> <span class="s1">'1.2.3'</span>
<span class="k">end</span>
<span class="n">gem</span> <span class="s1">'jquery-rails'</span><span class="p">,</span> <span class="s1">'2.0.2'</span>
<span class="n">group</span> <span class="ss">:production</span> <span class="k">do</span>
<span class="n">gem</span> <span class="s1">'pg'</span><span class="p">,</span> <span class="s1">'0.12.2'</span>
<span class="k">end</span>
</pre></div>
</div></div>
<p>Note that <a class="ref" href="a-demo-app.html#code-demo_gemfile_sqlite_version_redux">Listing 2.1</a> is identical to <a class="ref" href="beginning.html#code-gemfile_pg_gem">Listing 1.9</a>.</p>
<p>As in <a class="ref" href="beginning.html#sec-heroku_setup">Section 1.4.1</a>, we’ll install the local gems while suppressing the installation of production gems using the <tt class="verb">--without production</tt> option:</p>
<div class="code"><div class="highlight"><pre><span class="gp">$</span> bundle update
<span class="gp">$</span> bundle install --without production
</pre></div>
</div>
<p>Finally, we’ll put the demo app under version control. Recall that the <code>rails</code> command generates a default <code>.gitignore</code> file, but depending on your system you may find the augmented file from <a class="ref" href="beginning.html#code-gitignore">Listing 1.7</a> to be more convenient. Then initialize a Git repository and make the first commit:</p>
<div class="code"><div class="highlight"><pre><span class="gp">$</span> git init
<span class="gp">$</span> git add .
<span class="gp">$</span> git commit -m <span class="s2">"Initial commit"</span>
</pre></div>
</div>
<div class="label" id="fig-create_demo_repo"></div>
<div class="figure"><div class="center"><span class="graphic"><img src="images/figures/create_demo_repo_new.png" alt="create_demo_repo_new" /></span></div><div class="caption"><span class="header">Figure 2.1: </span><span class="description">Creating a demo app repository at GitHub. <a href="http://railstutorial.org/images/figures/create_demo_repo_new-full.png">(full size)</a></span></div></div>
<p>You can also optionally create a new repository (<a class="ref" href="a-demo-app.html#fig-create_demo_repo">Figure 2.1</a>) and push it up to GitHub:</p>
<div class="code"><div class="highlight"><pre><span class="gp">$</span> git remote add origin https://github.com/<username>/demo_app.git
<span class="gp">$</span> git push -u origin master
</pre></div>
</div>
<p>(As with the first app, take care <em>not</em> to initialize the GitHub repository with a <code>README</code> file.)</p>
<p>Now we’re ready to start making the app itself. The typical first step when making a web application is to create a <em>data model</em>, which is a representation of the structures needed by our application. In our case, the demo app will be a microblog, with only users and short (micro)posts. Thus, we’ll begin with a model for <em>users</em> of the app (<a class="ref" href="a-demo-app.html#sec-modeling_demo_users">Section 2.1.1</a>), and then we’ll add a model for <em>microposts</em> (<a class="ref" href="a-demo-app.html#sec-modeling_demo_microposts">Section 2.1.2</a>).</p>
<div class="label" id="sec-modeling_demo_users"></div>
<h3><a id="sec-2_1_1" href="a-demo-app.html#sec-modeling_demo_users" class="heading"><span class="number">2.1.1</span> Modeling demo users</a></h3>
<p>There are as many choices for a user data model as there are different registration forms on the web; we’ll go with a distinctly minimalist approach. Users of our demo app will have a unique <code>integer</code> identifier called <code>id</code>, a publicly viewable <code>name</code> (of type <code>string</code>), and an <code>email</code> address (also a <code>string</code>) that will double as a username. A summary of the data model for users appears in <a class="ref" href="a-demo-app.html#fig-demo_user_model">Figure 2.2</a>.</p>
<div class="label" id="fig-demo_user_model"></div>
<div class="figure"><div class="center"><span class="graphic"><img src="images/figures/demo_user_model.png" alt="demo_user_model" /></span></div><div class="caption"><span class="header">Figure 2.2: </span><span class="description">The data model for users.</span></div></div>
<p>As we’ll see starting in <a class="ref" href="modeling-users.html#sec-database_migrations">Section 6.1.1</a>, the label <code>users</code> in <a class="ref" href="a-demo-app.html#fig-demo_user_model">Figure 2.2</a> corresponds to a <em>table</em> in a database, and the <code>id</code>, <code>name</code>, and <code>email</code> attributes are <em>columns</em> in that table.</p>
<div class="label" id="sec-modeling_demo_microposts"></div>
<h3><a id="sec-2_1_2" href="a-demo-app.html#sec-modeling_demo_microposts" class="heading"><span class="number">2.1.2</span> Modeling demo microposts</a></h3>
<p>The core of the micropost data model is even simpler than the one for users: a micropost has only an <code>id</code> and a <code>content</code> field for the micropost’s text (of type <code>string</code>).<sup class="footnote" id="fnref-2_1"><a href="#fn-2_1">1</a></sup> There’s an additional complication, though: we want to <em>associate</em> each micropost with a particular user; we’ll accomplish this by recording the <code>user_id</code> of the owner of the post. The results are shown in <a class="ref" href="a-demo-app.html#fig-demo_micropost_model">Figure 2.3</a>.</p>
<div class="label" id="fig-demo_micropost_model"></div>
<div class="figure"><div class="center"><span class="graphic"><img src="images/figures/demo_micropost_model.png" alt="demo_micropost_model" /></span></div><div class="caption"><span class="header">Figure 2.3: </span><span class="description">The data model for microposts.</span></div></div>
<p>We’ll see in <a class="ref" href="a-demo-app.html#sec-demo_user_has_many_microposts">Section 2.3.3</a> (and more fully in <a class="ref" href="user-microposts.html#top">Chapter 10</a>) how this <code>user_id</code> attribute allows us to succinctly express the notion that a user potentially has many associated microposts.</p>
<div class="label" id="sec-demo_users_resource"></div>
<h2><a id="sec-2_2" href="a-demo-app.html#sec-demo_users_resource" class="heading"><span class="number">2.2</span> The Users resource</a></h2>
<p>In this section, we’ll implement the users data model in <a class="ref" href="a-demo-app.html#sec-modeling_demo_users">Section 2.1.1</a>, along with a web interface to that model.
The combination will constitute a <em>Users resource</em>, which will allow us to think of users as objects that can be created, read, updated, and deleted through the web via the <a href="http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol">HTTP protocol</a>. As promised in the introduction, our Users resource will be created by a scaffold generator program, which comes standard with each Rails project. I urge you not to look too closely at the generated code; at this stage, it will only serve to confuse you.</p>
<p>Rails scaffolding is generated by passing the <code>scaffold</code> command to the <code>rails generate</code> script. The argument of the <code>scaffold</code> command is the singular version of the resource name (in this case, <code>User</code>), together with optional parameters for the data model’s attributes:<sup class="footnote" id="fnref-2_2"><a href="#fn-2_2">2</a></sup></p>
<div class="code"><div class="highlight"><pre>$ rails generate scaffold User name:string email:string
invoke active_record
create db/migrate/20111123225336_create_users.rb
create app/models/user.rb
invoke test_unit
create test/unit/user_test.rb
create test/fixtures/users.yml
route resources :users
invoke scaffold_controller
create app/controllers/users_controller.rb
invoke erb
create app/views/users
create app/views/users/index.html.erb
create app/views/users/edit.html.erb
create app/views/users/show.html.erb
create app/views/users/new.html.erb
create app/views/users/_form.html.erb
invoke test_unit
create test/functional/users_controller_test.rb
invoke helper
create app/helpers/users_helper.rb
invoke test_unit
create test/unit/helpers/users_helper_test.rb
invoke assets
invoke coffee
create app/assets/javascripts/users.js.coffee
invoke scss
create app/assets/stylesheets/users.css.scss
invoke scss
create app/assets/stylesheets/scaffolds.css.scss
</pre></div>
</div>
<p>By including <code>name:string</code> and <code>email:string</code>, we have arranged for the User model to have the form shown in <a class="ref" href="a-demo-app.html#fig-demo_user_model">Figure 2.2</a>. (Note that there is no need to include a parameter for <code>id</code>; it is created automatically by Rails for use as the <em>primary key</em> in the database.)</p>
<p>To proceed with the demo application, we first need to <em>migrate</em> the database using <em>Rake</em> (<a class="ref" href="a-demo-app.html#sidebar-rake">Box 2.1</a>):</p>
<div class="code"><div class="highlight"><pre><span class="gp">$</span> bundle <span class="nb">exec </span>rake db:migrate
<span class="go">== CreateUsers: migrating ====================================================</span>
<span class="go">-- create_table(:users)</span>
<span class="go"> -> 0.0017s</span>
<span class="go">== CreateUsers: migrated (0.0018s) ===========================================</span>
</pre></div>
</div>
<p>This simply updates the database with our new <code>users</code> data model. (We’ll learn more about database migrations starting in <a class="ref" href="modeling-users.html#sec-database_migrations">Section 6.1.1</a>.) Note that, in order to ensure that the command uses the version of Rake corresponding to our <code>Gemfile</code>, we need to run <code>rake</code> using <code>bundle exec</code>.</p>
<p>With that, we can run the local web server using <code>rails s</code>, which is a shortcut for <code>rails server</code>:</p>
<div class="code"><div class="highlight"><pre><span class="gp">$</span> rails s
</pre></div>
</div>
<p>Now the demo application should be ready to go at <a href="http://localhost:3000/">http://localhost:3000/</a>.</p>
<div class="label" id="sidebar-rake"></div>
<div class="sidebar"><span class="title"><span class="header">Box 2.1.</span><span class="description">Rake</span></span>
<p>In the Unix tradition, the <a href="http://en.wikipedia.org/wiki/Make_(software)"><em>make</em></a> utility has played an important role in building executable programs from source code; many a computer hacker has committed to muscle memory the line</p>
<pre class="verbatim"> $ ./configure && make && sudo make install</pre>
<p>commonly used to compile code on Unix systems (including Linux and Mac OS X).</p>
<p>Rake is <em>Ruby make</em>, a make-like language written in Ruby. Rails uses Rake extensively, especially for the innumerable little administrative tasks necessary when developing database-backed web applications. The <code>rake db:migrate</code> command is probably the most common, but there are many others; you can see a list of database tasks using <code>-T db</code>:</p>
<pre class="verbatim"> $ bundle exec rake -T db</pre>
<p>To see all the Rake tasks available, run</p>
<pre class="verbatim"> $ bundle exec rake -T</pre>
<p>The list is likely to be overwhelming, but don’t worry, you don’t have to know all (or even most) of these commands. By the end of the <em>Rails Tutorial</em>, you’ll know all the most important ones.</p>
</div>
<div class="label" id="sec-a_user_tour"></div>
<h3><a id="sec-2_2_1" href="a-demo-app.html#sec-a_user_tour" class="heading"><span class="number">2.2.1</span> A user tour</a></h3>
<p>Visiting the root url <a href="http://localhost:3000/">http://localhost:3000/</a> shows the same default Rails page shown in <a class="ref" href="beginning.html#fig-riding_rails_31">Figure 1.3</a>, but in generating the Users resource scaffolding we have also created a large number of pages for manipulating users. For example, the page for listing all users is at <a href="http://localhost:3000/users">/users</a>, and the
page for making a new user is at <a href="http://localhost:3000/users/new">/users/new</a>. The rest of this section is dedicated to taking a whirlwind tour through these user pages. As we proceed, it may help to refer to <a class="ref" href="a-demo-app.html#table-user_urls">Table 2.1</a>, which shows the correspondence between pages and URIs.</p>
<div class="label" id="table-user_urls"></div>
<div class="table"><div class="center">
<table class="tabular"><tr><th class="align_left"><strong>URI</strong></th><th class="align_left"><strong>Action</strong></th><th class="align_left"><strong>Purpose</strong></th></tr><tr class="top_bar"><td class="align_left"><a href="http://localhost:3000/users">/users</a></td><td class="align_left"><code>index</code></td><td class="align_left">page to list all users</td></tr><tr><td class="align_left"><a href="http://localhost:3000/users/1">/users/1</a></td><td class="align_left"><code>show</code></td><td class="align_left">page to show user with id <code>1</code></td></tr><tr><td class="align_left"><a href="http://localhost:3000/users/new">/users/new</a></td><td class="align_left"><code>new</code></td><td class="align_left">page to make a new user</td></tr><tr><td class="align_left"><a href="http://localhost:3000/users/1/edit">/users/1/edit</a></td><td class="align_left"><code>edit</code></td><td class="align_left">page to edit user with id <code>1</code></td></tr></table></div><div class="caption"><span class="header">Table 2.1: </span><span class="description">The correspondence between pages and URIs for the Users resource.</span></div></div>
<p>We start with the page to show all the users in our application, called <a href="http://localhost:3000/users"><tt>index</tt></a>; as you might expect, initially there are no users at all (<a class="ref" href="a-demo-app.html#fig-demo_blank_user_index_rails_3">Figure 2.4</a>).</p>
<div class="label" id="fig-demo_blank_user_index_rails_3"></div>
<div class="figure"><div class="center"><span class="graphic"><img src="images/figures/demo_blank_user_index_rails_3.png" alt="demo_blank_user_index_rails_3" /></span></div><div class="caption"><span class="header">Figure 2.4: </span><span class="description">The initial index page for the Users resource (<a href="http://localhost:3000/users">/users</a>). <a href="http://railstutorial.org/images/figures/demo_blank_user_index_rails_3-full.png">(full size)</a></span></div></div>
<p>To make a new user, we visit the <a href="http://localhost:3000/users/new"><tt>new</tt></a> page, as shown in <a class="ref" href="a-demo-app.html#fig-demo_new_user_rails_3">Figure 2.5</a>. (Since the http://localhost:3000 part of the address is implicit whenever we are developing locally, I’ll usually omit it from now on.) In <a class="ref" href="sign-up.html#top">Chapter 7</a>, this will become the user signup page.</p>
<div class="label" id="fig-demo_new_user_rails_3"></div>
<div class="figure"><div class="center"><span class="graphic"><img src="images/figures/demo_new_user_rails_3.png" alt="demo_new_user_rails_3" /></span></div><div class="caption"><span class="header">Figure 2.5: </span><span class="description">The new user page (<a href="http://localhost:3000/users/new">/users/new</a>). <a href="http://railstutorial.org/images/figures/demo_new_user_rails_3-full.png">(full size)</a></span></div></div>
<p>We can create a user by entering name and email values in the text fields and then clicking the Create User button. The result is the user <a href="http://localhost:3000/users/1"><tt>show</tt></a> page, as seen in <a class="ref" href="a-demo-app.html#fig-demo_show_user_rails_3">Figure 2.6</a>. (The green welcome message is accomplished using the <em>flash</em>, which we’ll learn about in <a class="ref" href="sign-up.html#sec-the_flash">Section 7.4.2</a>.) Note that the URI is <a href="http://localhost:3000/users/1">/users/1</a>; as you might suspect, the number <code>1</code> is simply the user’s <code>id</code> attribute from <a class="ref" href="a-demo-app.html#fig-demo_user_model">Figure 2.2</a>. In <a class="ref" href="sign-up.html#sec-showing_users">Section 7.1</a>, this page will become the user’s profile.</p>
<div class="label" id="fig-demo_show_user_rails_3"></div>
<div class="figure"><div class="center"><span class="graphic"><img src="images/figures/demo_show_user_rails_3.png" alt="demo_show_user_rails_3" /></span></div><div class="caption"><span class="header">Figure 2.6: </span><span class="description">The page to show a user (<a href="http://localhost:3000/users/1">/users/1</a>). <a href="http://railstutorial.org/images/figures/demo_show_user_rails_3-full.png">(full size)</a></span></div></div>
<p>To change a user’s information, we visit the <a href="http://localhost:3000/users/1/edit"><tt>edit</tt></a> page (<a class="ref" href="a-demo-app.html#fig-demo_edit_user_rails_3">Figure 2.7</a>). By modifying the user information and clicking the Update User button, we arrange to change the information for the user in the demo application (<a class="ref" href="a-demo-app.html#fig-demo_update_user_rails_3">Figure 2.8</a>). (As we’ll see in detail starting in <a class="ref" href="modeling-users.html#top">Chapter 6</a>, this user data is stored in a database back-end.) We’ll add user edit/update functionality to the sample application in <a class="ref" href="updating-showing-and-deleting-users.html#sec-updating_users">Section 9.1</a>.</p>
<div class="label" id="fig-demo_edit_user_rails_3"></div>
<div class="figure"><div class="center"><span class="graphic"><img src="images/figures/demo_edit_user_rails_3.png" alt="demo_edit_user_rails_3" /></span></div><div class="caption"><span class="header">Figure 2.7: </span><span class="description">The user edit page (<a href="http://localhost:3000/users/1/edit">/users/1/edit</a>). <a href="http://railstutorial.org/images/figures/demo_edit_user_rails_3-full.png">(full size)</a></span></div></div>
<div class="label" id="fig-demo_update_user_rails_3"></div>
<div class="figure"><div class="center"><span class="graphic"><img src="images/figures/demo_update_user_rails_3.png" alt="demo_update_user_rails_3" /></span></div><div class="caption"><span class="header">Figure 2.8: </span><span class="description">A user with updated information. <a href="http://railstutorial.org/images/figures/demo_update_user_rails_3-full.png">(full size)</a></span></div></div>
<p>Now we’ll create a second user by revisiting the <a href="http://localhost:3000/users/new"><tt>new</tt></a> page and submitting a second set of user information; the resulting user <a href="http://localhost:3000/users"><tt>index</tt></a> is shown in <a class="ref" href="a-demo-app.html#fig-demo_user_index_two_rails_3">Figure 2.9</a>. <a class="ref" href="sign-up.html#sec-showing_users">Section 7.1</a> will develop the user index into a more polished page for showing all users.</p>
<div class="label" id="fig-demo_user_index_two_rails_3"></div>
<div class="figure"><div class="center"><span class="graphic"><img src="images/figures/demo_user_index_two_rails_3.png" alt="demo_user_index_two_rails_3" /></span></div><div class="caption"><span class="header">Figure 2.9: </span><span class="description">The user index page (<a href="http://localhost:3000/users">/users</a>) with a second user. <a href="http://railstutorial.org/images/figures/demo_user_index_two_rails_3-full.png">(full size)</a></span></div></div>
<p>Having shown how to create, show, and edit users, we come finally to destroying them (<a class="ref" href="a-demo-app.html#fig-demo_destroy_user_rails_3">Figure 2.10</a>). You should verify that clicking on the link in <a class="ref" href="a-demo-app.html#fig-demo_destroy_user_rails_3">Figure 2.10</a> destroys the second user, yielding an index page with only one user. (If it doesn’t work, be sure that JavaScript is enabled in your browser; Rails uses JavaScript to issue the request needed to destroy a user.) <a class="ref" href="updating-showing-and-deleting-users.html#sec-destroying_users">Section 9.4</a> adds user deletion to the sample app, taking care to restrict its use to a special class of administrative users.</p>
<div class="label" id="fig-demo_destroy_user_rails_3"></div>
<div class="figure"><div class="center"><span class="graphic"><img src="images/figures/demo_destroy_user_rails_3.png" alt="demo_destroy_user_rails_3" /></span></div><div class="caption"><span class="header">Figure 2.10: </span><span class="description">Destroying a user. <a href="http://railstutorial.org/images/figures/demo_destroy_user_rails_3-full.png">(full size)</a></span></div></div>
<div class="label" id="sec-mvc_in_action"></div>
<h3><a id="sec-2_2_2" href="a-demo-app.html#sec-mvc_in_action" class="heading"><span class="number">2.2.2</span> MVC in action</a></h3>
<p>Now that we’ve completed a quick overview of the Users resource, let’s examine one particular part of it in the context of the Model-View-Controller (MVC) pattern introduced in <a class="ref" href="beginning.html#sec-mvc">Section 1.2.6</a>. Our strategy will be to describe the results of a typical browser hit—a visit to the user index page at <a href="http://localhost:3000/users">/users</a>—in terms of MVC (<a class="ref" href="a-demo-app.html#fig-mvc_detailed">Figure 2.11</a>).</p>
<div class="label" id="fig-mvc_detailed"></div>
<div class="figure"><div class="center"><span class="graphic"><img src="images/figures/mvc_detailed.png" alt="mvc_detailed" /></span></div><div class="caption"><span class="header">Figure 2.11: </span><span class="description">A detailed diagram of MVC in Rails. <a href="http://railstutorial.org/images/figures/mvc_detailed-full.png">(full size)</a></span></div></div>
<ol>
<li>The browser issues a request for the /users URI.</li>
<li>Rails routes /users to the <code>index</code> action in the Users controller.</li>
<li>The <code>index</code> action asks the User model to retrieve all users (<code>User.all</code>).</li>
<li>The User model pulls all the users from the database.</li>
<li>The User model returns the list of users to the controller.</li>
<li>The controller captures the users in the <code>@users</code> variable, which is passed to the <code>index</code> view.</li>
<li>The view uses embedded Ruby to render the page as HTML.</li>
<li>The controller passes the HTML back to the browser.<sup class="footnote" id="fnref-2_3"><a href="#fn-2_3">3</a></sup></li>
</ol>
<p>We start with a request issued from the browser—i.e., the result of typing a URI in the address bar or clicking on a link (Step 1 in <a class="ref" href="a-demo-app.html#fig-mvc_detailed">Figure 2.11</a>). This request hits the <em>Rails router</em> (Step 2), which dispatches to the proper <em>controller action</em> based on the URI (and, as we’ll see in <a class="ref" href="static-pages.html#sidebar-get_etc">Box 3.2</a>, the type of request). The code to create the mapping of user URIs to controller actions for the Users resource appears in <a class="ref" href="a-demo-app.html#code-rails_routes">Listing 2.2</a>; this code effectively sets up the table of URI/action pairs seen in <a class="ref" href="a-demo-app.html#table-user_urls">Table 2.1</a>. (The strange notation <code>:users</code> is a <em>symbol</em>, which we’ll learn about in <a class="ref" href="rails-flavored-ruby.html#sec-hashes_and_symbols">Section 4.3.3</a>.)</p>
<div class="label" id="code-rails_routes"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 2.2.</span> <span class="description">The Rails routes, with a rule for the Users resource. <br /> <code>config/routes.rb</code></span> </div>
<div class="code"><div class="highlight"><pre><span class="ss">DemoApp::Application</span><span class="o">.</span><span class="n">routes</span><span class="o">.</span><span class="n">draw</span> <span class="k">do</span>
<span class="n">resources</span> <span class="ss">:users</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="k">end</span>
</pre></div>
</div></div>
<p>The pages from the tour in <a class="ref" href="a-demo-app.html#sec-a_user_tour">Section 2.2.1</a> correspond to <em>actions</em> in the Users <em>controller</em>, which is a collection of related actions; the controller generated by the scaffolding is shown schematically in <a class="ref" href="a-demo-app.html#code-demo_users_controller">Listing 2.3</a>. Note the notation <code>class UsersController < ApplicationController</code>; this is an example of a Ruby <em>class</em> with <em>inheritance</em>. (We’ll discuss inheritance briefly in <a class="ref" href="a-demo-app.html#sec-inheritance_hierarchies">Section 2.3.4</a> and cover both subjects in more detail in <a class="ref" href="rails-flavored-ruby.html#sec-ruby_classes">Section 4.4</a>.)</p>
<div class="label" id="code-demo_users_controller"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 2.3.</span> <span class="description">The Users controller in schematic form. <br /> <code>app/controllers/users_controller.rb</code></span> </div>
<div class="code"><div class="highlight"><pre><span class="k">class</span> <span class="nc">UsersController</span> <span class="o"><</span> <span class="no">ApplicationController</span>
<span class="k">def</span> <span class="nf">index</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="k">end</span>
<span class="k">def</span> <span class="nf">show</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="k">end</span>
<span class="k">def</span> <span class="nf">new</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="k">end</span>
<span class="k">def</span> <span class="nf">create</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="k">end</span>
<span class="k">def</span> <span class="nf">edit</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="k">end</span>
<span class="k">def</span> <span class="nf">update</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="k">end</span>
<span class="k">def</span> <span class="nf">destroy</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="k">end</span>
<span class="k">end</span>
</pre></div>
</div></div>
<p>You may notice that there are more actions than there are pages; the <code>index</code>, <code>show</code>, <code>new</code>, and <code>edit</code> actions all correspond to pages from <a class="ref" href="a-demo-app.html#sec-a_user_tour">Section 2.2.1</a>, but there are additional <code>create</code>, <code>update</code>, and <code>destroy</code> actions as well. These actions don’t typically render pages (although they sometimes do); instead, their main purpose is to modify information about users in the database. This full suite of controller actions, summarized in <a class="ref" href="a-demo-app.html#table-demo_RESTful_users">Table 2.2</a>, represents the implementation of the REST architecture in Rails (<a class="ref" href="a-demo-app.html#sidebar-REST">Box 2.2</a>), which is based on the ideas of <em>representational state transfer</em> identified and named by computer scientist <a href="http://en.wikipedia.org/wiki/Roy_Fielding">Roy Fielding</a>.<sup class="footnote" id="fnref-2_4"><a href="#fn-2_4">4</a></sup> Note from <a class="ref" href="a-demo-app.html#table-demo_RESTful_users">Table 2.2</a> that there is some overlap in the URIs; for example, both the user <code>show</code> action and the <code>update</code> action correspond to the URI /users/1. The difference between them is the <a href="http://en.wikipedia.org/wiki/HTTP_request#Request_methods">HTTP request method</a> they respond to. We’ll learn more about HTTP request methods starting in <a class="ref" href="static-pages.html#sec-TDD">Section 3.2.1</a>.</p>
<div class="label" id="table-demo_RESTful_users"></div>
<div class="table"><div class="center">
<table class="tabular"><tr><th class="align_left"><strong>HTTP request</strong></th><th class="align_left"><strong>URI</strong></th><th class="align_left"><strong>Action</strong></th><th class="align_left"><strong>Purpose</strong></th></tr><tr class="top_bar"><td class="align_left"><tt>GET</tt></td><td class="align_left">/users</td><td class="align_left"><code>index</code></td><td class="align_left">page to list all users</td></tr><tr><td class="align_left"><tt>GET</tt></td><td class="align_left">/users/1</td><td class="align_left"><code>show</code></td><td class="align_left">page to show user with id <code>1</code></td></tr><tr><td class="align_left"><tt>GET</tt></td><td class="align_left">/users/new</td><td class="align_left"><code>new</code></td><td class="align_left">page to make a new user</td></tr><tr><td class="align_left"><tt>POST</tt></td><td class="align_left">/users</td><td class="align_left"><code>create</code></td><td class="align_left">create a new user</td></tr><tr><td class="align_left"><tt>GET</tt></td><td class="align_left">/users/1/edit</td><td class="align_left"><code>edit</code></td><td class="align_left">page to edit user with id <code>1</code></td></tr><tr><td class="align_left"><tt>PUT</tt></td><td class="align_left">/users/1</td><td class="align_left"><code>update</code></td><td class="align_left">update user with id <code>1</code></td></tr><tr><td class="align_left"><tt>DELETE</tt></td><td class="align_left">/users/1</td><td class="align_left"><code>destroy</code></td><td class="align_left">delete user with id <code>1</code></td></tr></table></div><div class="caption"><span class="header">Table 2.2: </span><span class="description">RESTful routes provided by the Users resource in <a class="ref" href="a-demo-app.html#code-rails_routes">Listing 2.2</a>.</span></div></div>
<div class="label" id="sidebar-REST"></div>
<div class="sidebar"><span class="title"><span class="header">Box 2.2.</span><span class="description">REpresentational State Transfer (REST)</span></span>
<p>If you read much about Ruby on Rails web development, you’ll see a lot of references to “REST”, which is an acronym for REpresentational State Transfer. REST is an architectural style for developing distributed, networked systems and software applications such as the World Wide Web and web applications. Although REST theory is rather abstract, in the context of Rails applications REST means that most application components (such as users and microposts) are modeled as <em>resources</em> that can be created, read, updated, and deleted—operations that correspond both to the <a href="http://en.wikipedia.org/wiki/Create,_read,_update_and_delete">CRUD operations of relational databases</a> and the four fundamental <a href="http://en.wikipedia.org/wiki/HTTP_request#Request_methods">HTTP request methods</a>: <tt>POST</tt>, <tt>GET</tt>, <tt>PUT</tt>, and <tt>DELETE</tt>. (We’ll learn more about HTTP requests in <a class="ref" href="static-pages.html#sec-TDD">Section 3.2.1</a> and especially <a class="ref" href="static-pages.html#sidebar-get_etc">Box 3.2</a>.)</p>
<p>As a Rails application developer, the RESTful style of development helps you make choices about which controllers and actions to write: you simply structure the application using resources that get created, read, updated, and deleted. In the case of users and microposts, this process is straightforward, since they are naturally resources in their own right. In <a class="ref" href="following-users.html#top">Chapter 11</a>, we’ll see an example where REST principles allow us to model a subtler problem, “following users”, in a natural and convenient way.</p>
</div>
<p>To examine the relationship between the Users controller and the User model, let’s focus on a simplified version of the <code>index</code> action, shown in <a class="ref" href="a-demo-app.html#code-demo_index_action">Listing 2.4</a>. (The scaffold code is ugly and confusing, so I’ve suppressed it.)</p>
<div class="label" id="code-demo_index_action"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 2.4.</span> <span class="description">The simplified user <code>index</code> action for the demo application. <br /> <code>app/controllers/users_controller.rb</code></span> </div>
<div class="code"><div class="highlight"><pre><span class="k">class</span> <span class="nc">UsersController</span> <span class="o"><</span> <span class="no">ApplicationController</span>
<span class="k">def</span> <span class="nf">index</span>
<span class="vi">@users</span> <span class="o">=</span> <span class="no">User</span><span class="o">.</span><span class="n">all</span>
<span class="k">end</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="k">end</span>
</pre></div>
</div></div>
<p>This <code>index</code> action has the line <code>@users = User.all</code> (Step 3), which asks the User model to retrieve a list of all the users from the database (Step 4), and then places them in the variable <code>@users</code> (pronounced “at-users”) (Step 5). The User model itself appears in <a class="ref" href="a-demo-app.html#code-demo_user_model">Listing 2.5</a>; although it is rather plain, it comes equipped with a large amount of functionality because of inheritance (<a class="ref" href="a-demo-app.html#sec-inheritance_hierarchies">Section 2.3.4</a> and <a class="ref" href="rails-flavored-ruby.html#sec-ruby_classes">Section 4.4</a>). In particular, by using the Rails library called <em>Active Record</em>, the code in <a class="ref" href="a-demo-app.html#code-demo_user_model">Listing 2.5</a> arranges for <code>User.all</code> to return all the users. (We’ll learn about the <code>attr_accessible</code> line in <a class="ref" href="modeling-users.html#sec-accessible_attributes">Section 6.1.2.2</a>. <em>Note</em>: This line will not appear if you are using Rails 3.2.2 or earlier.)</p>
<div class="label" id="code-demo_user_model"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 2.5.</span> <span class="description">The User model for the demo application. <br /> <code>app/models/user.rb</code></span> </div>
<div class="code"><div class="highlight"><pre><span class="k">class</span> <span class="nc">User</span> <span class="o"><</span> <span class="ss">ActiveRecord::Base</span>
<span class="n">attr_accessible</span> <span class="ss">:email</span><span class="p">,</span> <span class="ss">:name</span>
<span class="k">end</span>
</pre></div>
</div></div>
<p>Once the <code>@users</code> variable is defined, the controller calls the <em>view</em> (Step 6), shown in <a class="ref" href="a-demo-app.html#code-demo_index_view">Listing 2.6</a>. Variables that start with the <code>@</code> sign, called <em>instance variables</em>, are automatically available in the view; in this case, the <code>index.html.erb</code> view in <a class="ref" href="a-demo-app.html#code-demo_index_view">Listing 2.6</a> iterates through the <code>@users</code> list and outputs a line of HTML for each one. (Remember, you aren’t supposed to understand this code right now. It is shown only for purposes of illustration.)</p>
<div class="label" id="code-demo_index_view"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 2.6.</span> <span class="description">The view for the user index. <br /> <code>app/views/users/index.html.erb</code></span> </div>
<div class="code"><div class="highlight"><pre><span class="nt"><h1></span>Listing users<span class="nt"></h1></span>
<span class="nt"><table></span>
<span class="nt"><tr></span>
<span class="nt"><th></span>Name<span class="nt"></th></span>
<span class="nt"><th></span>Email<span class="nt"></th></span>
<span class="nt"><th></th></span>
<span class="nt"><th></th></span>
<span class="nt"><th></th></span>
<span class="nt"></tr></span>
<span class="cp"><%</span> <span class="vi">@users</span><span class="o">.</span><span class="n">each</span> <span class="k">do</span> <span class="o">|</span><span class="n">user</span><span class="o">|</span> <span class="cp">%></span>
<span class="nt"><tr></span>
<span class="nt"><td></span><span class="cp"><%=</span> <span class="n">user</span><span class="o">.</span><span class="n">name</span> <span class="cp">%></span><span class="nt"></td></span>
<span class="nt"><td></span><span class="cp"><%=</span> <span class="n">user</span><span class="o">.</span><span class="n">email</span> <span class="cp">%></span><span class="nt"></td></span>
<span class="nt"><td></span><span class="cp"><%=</span> <span class="n">link_to</span> <span class="s1">'Show'</span><span class="p">,</span> <span class="n">user</span> <span class="cp">%></span><span class="nt"></td></span>
<span class="nt"><td></span><span class="cp"><%=</span> <span class="n">link_to</span> <span class="s1">'Edit'</span><span class="p">,</span> <span class="n">edit_user_path</span><span class="p">(</span><span class="n">user</span><span class="p">)</span> <span class="cp">%></span><span class="nt"></td></span>
<span class="nt"><td></span><span class="cp"><%=</span> <span class="n">link_to</span> <span class="s1">'Destroy'</span><span class="p">,</span> <span class="n">user</span><span class="p">,</span> <span class="nb">method</span><span class="p">:</span> <span class="ss">:delete</span><span class="p">,</span>
<span class="ss">data:</span> <span class="p">{</span> <span class="ss">confirm:</span> <span class="s1">'Are you sure?'</span> <span class="p">}</span> <span class="cp">%></span><span class="nt"></td></span>
<span class="nt"></tr></span>
<span class="cp"><%</span> <span class="k">end</span> <span class="cp">%></span>
<span class="nt"></table></span>
<span class="nt"><br</span> <span class="nt">/></span>
<span class="cp"><%=</span> <span class="n">link_to</span> <span class="s1">'New User'</span><span class="p">,</span> <span class="n">new_user_path</span> <span class="cp">%></span>
</pre></div>
</div></div>
<p>The view converts its contents to HTML (Step 7), which is then returned by the controller to the browser for display (Step 8).</p>
<div class="label" id="sec-weaknesses_of_this_users_resource"></div>
<h3><a id="sec-2_2_3" href="a-demo-app.html#sec-weaknesses_of_this_users_resource" class="heading"><span class="number">2.2.3</span> Weaknesses of this Users resource</a></h3>
<p>Though good for getting a general overview of Rails, the scaffold Users resource suffers from a number of severe weaknesses.</p>
<ul>
<li><strong>No data validations.</strong> Our User model accepts data such as blank names and invalid email addresses without complaint.</li>
<li><strong>No authentication.</strong> We have no notion of signing in or out, and no way to prevent any user from performing any operation.</li>
<li><strong>No tests.</strong> This isn’t technically true—the scaffolding includes rudimentary tests—but the generated tests are ugly and inflexible, and they don’t test for data validation, authentication, or any other custom requirements.</li>
<li><strong>No layout.</strong> There is no consistent site styling or navigation.</li>
<li><strong>No real understanding.</strong> If you understand the scaffold code, you probably shouldn’t be reading this book.</li>
</ul>
<div class="label" id="sec-microposts_resource"></div>
<h2><a id="sec-2_3" href="a-demo-app.html#sec-microposts_resource" class="heading"><span class="number">2.3</span> The Microposts resource</a></h2>
<p>Having generated and explored the Users resource, we turn now to the associated Microposts resource. Throughout this section, I recommend comparing the elements of the Microposts resource with the analogous user elements from <a class="ref" href="a-demo-app.html#sec-demo_users_resource">Section 2.2</a>; you should see that the two resources parallel each other in many ways. The RESTful structure of Rails applications is best absorbed by this sort of repetition of form; indeed, seeing the parallel structure of Users and Microposts even at this early stage is one of the prime motivations for this chapter. (As we’ll see, writing applications more robust than the toy example in this chapter takes considerable effort—we won’t see the Microposts resource again until <a class="ref" href="user-microposts.html#top">Chapter 10</a>—and I didn’t want to defer its first appearance quite that far.)</p>
<div class="label" id="sec-a_micropost_microtour"></div>
<h3><a id="sec-2_3_1" href="a-demo-app.html#sec-a_micropost_microtour" class="heading"><span class="number">2.3.1</span> A micropost microtour</a></h3>
<p>As with the Users resource, we’ll generate scaffold code for the Microposts resource using <code>rails generate scaffold</code>, in this case implementing the data model from <a class="ref" href="a-demo-app.html#fig-demo_micropost_model">Figure 2.3</a>:<sup class="footnote" id="fnref-2_5"><a href="#fn-2_5">5</a></sup></p>
<div class="code"><div class="highlight"><pre>$ rails generate scaffold Micropost content:string user_id:integer
invoke active_record
create db/migrate/20111123225811_create_microposts.rb
create app/models/micropost.rb
invoke test_unit
create test/unit/micropost_test.rb
create test/fixtures/microposts.yml
route resources :microposts
invoke scaffold_controller
create app/controllers/microposts_controller.rb
invoke erb
create app/views/microposts
create app/views/microposts/index.html.erb
create app/views/microposts/edit.html.erb
create app/views/microposts/show.html.erb
create app/views/microposts/new.html.erb
create app/views/microposts/_form.html.erb
invoke test_unit
create test/functional/microposts_controller_test.rb
invoke helper
create app/helpers/microposts_helper.rb
invoke test_unit
create test/unit/helpers/microposts_helper_test.rb
invoke assets
invoke coffee
create app/assets/javascripts/microposts.js.coffee
invoke scss
create app/assets/stylesheets/microposts.css.scss
invoke scss
identical app/assets/stylesheets/scaffolds.css.scss
</pre></div>
</div>
<p>To update our database with the new data model, we need to run a migration as in <a class="ref" href="a-demo-app.html#sec-demo_users_resource">Section 2.2</a>:</p>
<div class="code"><div class="highlight"><pre><span class="gp">$</span> bundle <span class="nb">exec </span>rake db:migrate
<span class="go">== CreateMicroposts: migrating ===============================================</span>
<span class="go">-- create_table(:microposts)</span>
<span class="go"> -> 0.0023s</span>
<span class="go">== CreateMicroposts: migrated (0.0026s) ======================================</span>
</pre></div>
</div>
<p>Now we are in a position to create microposts in the same way we created users in <a class="ref" href="a-demo-app.html#sec-a_user_tour">Section 2.2.1</a>. As you might guess, the scaffold generator has updated the Rails routes file with a rule for Microposts resource, as seen in <a class="ref" href="a-demo-app.html#code-demo_microposts_resource">Listing 2.7</a>.<sup class="footnote" id="fnref-2_6"><a href="#fn-2_6">6</a></sup> As with users, the <code>resources :microposts</code> routing rule maps micropost URIs to actions in the Microposts controller, as seen in <a class="ref" href="a-demo-app.html#table-demo_RESTful_microposts">Table 2.3</a>.</p>
<div class="label" id="code-demo_microposts_resource"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 2.7.</span> <span class="description">The Rails routes, with a new rule for Microposts resources. <br /> <code>config/routes.rb</code></span> </div>
<div class="code"><div class="highlight"><pre><span class="ss">DemoApp::Application</span><span class="o">.</span><span class="n">routes</span><span class="o">.</span><span class="n">draw</span> <span class="k">do</span>
<span class="n">resources</span> <span class="ss">:microposts</span>
<span class="n">resources</span> <span class="ss">:users</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="k">end</span>
</pre></div>
</div></div>
<div class="label" id="table-demo_RESTful_microposts"></div>
<div class="table"><div class="center">
<table class="tabular"><tr><th class="align_left"><strong>HTTP request</strong></th><th class="align_left"><strong>URI</strong></th><th class="align_left"><strong>Action</strong></th><th class="align_left"><strong>Purpose</strong></th></tr><tr class="top_bar"><td class="align_left"><tt>GET</tt></td><td class="align_left">/microposts</td><td class="align_left"><code>index</code></td><td class="align_left">page to list all microposts</td></tr><tr><td class="align_left"><tt>GET</tt></td><td class="align_left">/microposts/1</td><td class="align_left"><code>show</code></td><td class="align_left">page to show micropost with id <code>1</code></td></tr><tr><td class="align_left"><tt>GET</tt></td><td class="align_left">/microposts/new</td><td class="align_left"><code>new</code></td><td class="align_left">page to make a new micropost</td></tr><tr><td class="align_left"><tt>POST</tt></td><td class="align_left">/microposts</td><td class="align_left"><code>create</code></td><td class="align_left">create a new micropost</td></tr><tr><td class="align_left"><tt>GET</tt></td><td class="align_left">/microposts/1/edit</td><td class="align_left"><code>edit</code></td><td class="align_left">page to edit micropost with id <code>1</code></td></tr><tr><td class="align_left"><tt>PUT</tt></td><td class="align_left">/microposts/1</td><td class="align_left"><code>update</code></td><td class="align_left">update micropost with id <code>1</code></td></tr><tr><td class="align_left"><tt>DELETE</tt></td><td class="align_left">/microposts/1</td><td class="align_left"><code>destroy</code></td><td class="align_left">delete micropost with id <code>1</code></td></tr></table></div><div class="caption"><span class="header">Table 2.3: </span><span class="description">RESTful routes provided by the Microposts resource in <a class="ref" href="a-demo-app.html#code-demo_microposts_resource">Listing 2.7</a>.</span></div></div>
<p>The Microposts controller itself appears in schematic form <a class="ref" href="a-demo-app.html#code-demo_microposts_controller">Listing 2.8</a>. Note that, apart from having <code>MicropostsController</code> in place of <code>UsersController</code>, <a class="ref" href="a-demo-app.html#code-demo_microposts_controller">Listing 2.8</a> is <em>identical</em> to the code in <a class="ref" href="a-demo-app.html#code-demo_users_controller">Listing 2.3</a>. This is a reflection of the REST architecture common to both resources.</p>
<div class="label" id="code-demo_microposts_controller"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 2.8.</span> <span class="description">The Microposts controller in schematic form. <br /> <code>app/controllers/microposts_controller.rb</code></span> </div>
<div class="code"><div class="highlight"><pre><span class="k">class</span> <span class="nc">MicropostsController</span> <span class="o"><</span> <span class="no">ApplicationController</span>
<span class="k">def</span> <span class="nf">index</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="k">end</span>
<span class="k">def</span> <span class="nf">show</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="k">end</span>
<span class="k">def</span> <span class="nf">new</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="k">end</span>
<span class="k">def</span> <span class="nf">create</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="k">end</span>
<span class="k">def</span> <span class="nf">edit</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="k">end</span>
<span class="k">def</span> <span class="nf">update</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="k">end</span>
<span class="k">def</span> <span class="nf">destroy</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="k">end</span>
<span class="k">end</span>
</pre></div>
</div></div>
<p>To make some actual microposts, we enter information at the new microposts page, <a href="http://localhost:3000/microposts/new">/microposts/new</a>, as seen in <a class="ref" href="a-demo-app.html#fig-demo_new_micropost_rails_3">Figure 2.12</a>.</p>
<div class="label" id="fig-demo_new_micropost_rails_3"></div>
<div class="figure"><div class="center"><span class="graphic"><img src="images/figures/demo_new_micropost_rails_3.png" alt="demo_new_micropost_rails_3" /></span></div><div class="caption"><span class="header">Figure 2.12: </span><span class="description">The new micropost page (<a href="http://localhost:3000/microposts/new">/microposts/new</a>). <a href="http://railstutorial.org/images/figures/demo_new_micropost-full.png">(full size)</a></span></div></div>
<p>At this point, go ahead and create a micropost or two, taking care to make sure that at least one has a <code>user_id</code> of <code>1</code> to match the id of the first user created in <a class="ref" href="a-demo-app.html#sec-a_user_tour">Section 2.2.1</a>. The result should look something like <a class="ref" href="a-demo-app.html#fig-demo_micropost_index_rails_3">Figure 2.13</a>.</p>
<div class="label" id="fig-demo_micropost_index_rails_3"></div>
<div class="figure"><div class="center"><span class="graphic"><img src="images/figures/demo_micropost_index_rails_3.png" alt="demo_micropost_index_rails_3" /></span></div><div class="caption"><span class="header">Figure 2.13: </span><span class="description">The micropost index page (<a href="http://localhost:3000/microposts">/microposts</a>). <a href="http://railstutorial.org/images/figures/demo_micropost_index_rails_3-full.png">(full size)</a></span></div></div>
<div class="label" id="sec-putting_the_micro_in_microposts"></div>
<h3><a id="sec-2_3_2" href="a-demo-app.html#sec-putting_the_micro_in_microposts" class="heading"><span class="number">2.3.2</span> Putting the <em>micro</em> in microposts</a></h3>
<p>Any <em>micro</em>post worthy of the name should have some means of enforcing the length of the post. Implementing this constraint in Rails is easy with <em>validations</em>; to accept microposts with at most 140 characters (à la Twitter), we use a <em>length</em> validation. At this point, you should open the file <code>app/models/micropost.rb</code> in your text editor or IDE and fill it with the contents of <a class="ref" href="a-demo-app.html#code-demo_length_validation">Listing 2.9</a>. (The use of <code>validates</code> in <a class="ref" href="a-demo-app.html#code-demo_length_validation">Listing 2.9</a> is characteristic of Rails 3; if you’ve previously worked with Rails 2.3, you should compare this to the use of <code>validates_length_of</code>.)</p>
<div class="label" id="code-demo_length_validation"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 2.9.</span> <span class="description">Constraining microposts to be at most 140 characters. <br /> <code>app/models/micropost.rb</code></span> </div>
<div class="code"><div class="highlight"><pre><span class="k">class</span> <span class="nc">Micropost</span> <span class="o"><</span> <span class="ss">ActiveRecord::Base</span>
<span class="n">attr_accessible</span> <span class="ss">:content</span><span class="p">,</span> <span class="ss">:user_id</span>
<span class="n">validates</span> <span class="ss">:content</span><span class="p">,</span> <span class="ss">:length</span> <span class="o">=></span> <span class="p">{</span> <span class="ss">:maximum</span> <span class="o">=></span> <span class="mi">140</span> <span class="p">}</span>
<span class="k">end</span>
</pre></div>
</div></div>
<p>The code in <a class="ref" href="a-demo-app.html#code-demo_length_validation">Listing 2.9</a> may look rather mysterious—we’ll cover validations more thoroughly starting in <a class="ref" href="modeling-users.html#sec-user_validations">Section 6.2</a>—but its effects are readily apparent if we go to the new micropost page and enter more than 140 characters for the content of the post. As seen in <a class="ref" href="a-demo-app.html#fig-micropost_length_error_rails_3">Figure 2.14</a>, Rails renders <em>error messages</em> indicating that the micropost’s content is too long. (We’ll learn more about error messages in <a class="ref" href="sign-up.html#sec-signup_error_messages">Section 7.3.2</a>.)</p>
<div class="label" id="fig-micropost_length_error_rails_3"></div>
<div class="figure"><div class="center"><span class="graphic"><img src="images/figures/micropost_length_error_rails_3.png" alt="micropost_length_error_rails_3" /></span></div><div class="caption"><span class="header">Figure 2.14: </span><span class="description">Error messages for a failed micropost creation. <a href="http://railstutorial.org/images/figures/micropost_length_error_rails_3-full.png">(full size)</a></span></div></div>
<div class="label" id="sec-demo_user_has_many_microposts"></div>
<h3><a id="sec-2_3_3" href="a-demo-app.html#sec-demo_user_has_many_microposts" class="heading"><span class="number">2.3.3</span> A user <tt>has_many</tt> microposts</a></h3>
<p>One of the most powerful features of Rails is the ability to form <em>associations</em> between different data models. In the case of our User model, each user potentially has many microposts. We can express this in code by updating the User and Micropost models as in <a class="ref" href="a-demo-app.html#code-demo_user_has_many_microposts">Listing 2.10</a> and <a class="ref" href="a-demo-app.html#code-demo_micropost_belongs_to_user">Listing 2.11</a>.</p>
<div class="label" id="code-demo_user_has_many_microposts"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 2.10.</span> <span class="description">A user has many microposts. <br /> <code>app/models/user.rb</code></span> </div>
<div class="code"><div class="highlight"><pre><span class="k">class</span> <span class="nc">User</span> <span class="o"><</span> <span class="ss">ActiveRecord::Base</span>
<span class="n">attr_accessible</span> <span class="ss">:email</span><span class="p">,</span> <span class="ss">:name</span>
<span class="n">has_many</span> <span class="ss">:microposts</span>
<span class="k">end</span>
</pre></div>
</div></div>
<div class="label" id="code-demo_micropost_belongs_to_user"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 2.11.</span> <span class="description">A micropost belongs to a user. <br /> <code>app/models/micropost.rb</code></span> </div>
<div class="code"><div class="highlight"><pre><span class="k">class</span> <span class="nc">Micropost</span> <span class="o"><</span> <span class="ss">ActiveRecord::Base</span>
<span class="n">attr_accessible</span> <span class="ss">:content</span><span class="p">,</span> <span class="ss">:user_id</span>
<span class="n">belongs_to</span> <span class="ss">:user</span>
<span class="n">validates</span> <span class="ss">:content</span><span class="p">,</span> <span class="ss">:length</span> <span class="o">=></span> <span class="p">{</span> <span class="ss">:maximum</span> <span class="o">=></span> <span class="mi">140</span> <span class="p">}</span>
<span class="k">end</span>
</pre></div>
</div></div>
<p>We can visualize the result of this association in <a class="ref" href="a-demo-app.html#fig-micropost_user_association">Figure 2.15</a>. Because of the <code>user_id</code> column in the <code>microposts</code> table, Rails (using Active Record) can infer the microposts associated with each user.</p>
<div class="label" id="fig-micropost_user_association"></div>
<div class="figure"><div class="center"><span class="graphic"><img src="images/figures/micropost_user_association.png" alt="micropost_user_association" /></span></div><div class="caption"><span class="header">Figure 2.15: </span><span class="description">The association between microposts and users.</span></div></div>
<p>In <a class="ref" href="user-microposts.html#top">Chapter 10</a> and <a class="ref" href="following-users.html#top">Chapter 11</a>, we will use the association of users and microposts both to display all a user’s microposts and to construct a Twitter-like micropost feed. For now, we can examine the implications of the user-micropost association by using the <em>console</em>, which is a useful tool for interacting with Rails applications. We first invoke the console with <code>rails console</code> at the command line, and then retrieve the first user from the database using <code>User.first</code> (putting the results in the variable <code>first_user</code>):<sup class="footnote" id="fnref-2_7"><a href="#fn-2_7">7</a></sup></p>
<div class="code"><div class="highlight"><pre><span class="go">$ rails console</span>
<span class="gp">>> </span><span class="n">first_user</span> <span class="o">=</span> <span class="no">User</span><span class="o">.</span><span class="n">first</span>
<span class="go">=> #<User id: 1, name: "Michael Hartl", email: "[email protected]",</span>
<span class="go">created_at: "2011-11-03 02:01:31", updated_at: "2011-11-03 02:01:31"></span>
<span class="gp">>> </span><span class="n">first_user</span><span class="o">.</span><span class="n">microposts</span>
<span class="go">=> [#<Micropost id: 1, content: "First micropost!", user_id: 1, created_at:</span>
<span class="go">"2011-11-03 02:37:37", updated_at: "2011-11-03 02:37:37">, #<Micropost id: 2,</span>
<span class="go">content: "Second micropost", user_id: 1, created_at: "2011-11-03 02:38:54",</span>
<span class="go">updated_at: "2011-11-03 02:38:54">]</span>
<span class="gp">>> </span><span class="nb">exit</span>
</pre></div>
</div>
<p>(I include the last line just to demonstrate how to exit the console, and on most systems you can Ctrl-d for the same purpose.) Here we have accessed the user’s microposts using the code <code>first_user.microposts</code>: with this code, Active Record automatically returns all the microposts with <code>user_id</code> equal to the id of <code>first_user</code> (in this case, <code>1</code>). We’ll learn much more about the association facilities in Active Record in <a class="ref" href="user-microposts.html#top">Chapter 10</a> and <a class="ref" href="following-users.html#top">Chapter 11</a>.</p>
<div class="label" id="sec-inheritance_hierarchies"></div>
<h3><a id="sec-2_3_4" href="a-demo-app.html#sec-inheritance_hierarchies" class="heading"><span class="number">2.3.4</span> Inheritance hierarchies</a></h3>
<p>We end our discussion of the demo application with a brief description of the controller and model class hierarchies in Rails. This discussion will only make much sense if you have some experience with object-oriented programming (OOP); if you haven’t studied OOP, feel free to skip this section. In particular, if you are unfamiliar with <em>classes</em> (discussed in <a class="ref" href="rails-flavored-ruby.html#sec-ruby_classes">Section 4.4</a>), I suggest looping back to this section at a later time.</p>
<p>We start with the inheritance structure for models. Comparing <a class="ref" href="a-demo-app.html#code-demo_user_class">Listing 2.12</a> and <a class="ref" href="a-demo-app.html#code-demo_micropost_class">Listing 2.13</a>, we see that both the User model and the Micropost model inherit (via the left angle bracket <code><</code>) from <code>ActiveRecord::Base</code>, which is the base class for models provided by ActiveRecord; a diagram summarizing this relationship appears in <a class="ref" href="a-demo-app.html#fig-demo_model_inheritance">Figure 2.16</a>. It is by inheriting from <code>ActiveRecord::Base</code> that our model objects gain the ability to communicate with the database, treat the database columns as Ruby attributes, and so on.</p>
<div class="label" id="code-demo_user_class"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 2.12.</span> <span class="description">The <code>User</code> class, with inheritance. <br /> <code>app/models/user.rb</code></span> </div>
<div class="code"><div class="highlight"><pre><span class="k">class</span> <span class="nc">User</span> <span class="o"><</span> <span class="ss">ActiveRecord::Base</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="k">end</span>
</pre></div>
</div></div>
<div class="label" id="code-demo_micropost_class"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 2.13.</span> <span class="description">The <code>Micropost</code> class, with inheritance. <br /> <code>app/models/micropost.rb</code></span> </div>
<div class="code"><div class="highlight"><pre><span class="k">class</span> <span class="nc">Micropost</span> <span class="o"><</span> <span class="ss">ActiveRecord::Base</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="k">end</span>
</pre></div>
</div></div>
<div class="label" id="fig-demo_model_inheritance"></div>
<div class="figure"><div class="center"><span class="graphic"><img src="images/figures/demo_model_inheritance.png" alt="demo_model_inheritance" /></span></div><div class="caption"><span class="header">Figure 2.16: </span><span class="description">The inheritance hierarchy for the User and Micropost models.</span></div></div>
<p>The inheritance structure for controllers is only slightly more complicated. Comparing <a class="ref" href="a-demo-app.html#code-demo_users_controller_class">Listing 2.14</a> and <a class="ref" href="a-demo-app.html#code-demo_microposts_controller_class">Listing 2.15</a>, we see that both the Users controller and the Microposts controller inherit from the Application controller. Examining <a class="ref" href="a-demo-app.html#code-demo_application_controller_class">Listing 2.16</a>, we see that <code>ApplicationController</code> itself inherits from <code>ActionController::Base</code>; this is the base class for controllers provided by the Rails library Action Pack. The relationships between these classes is illustrated in <a class="ref" href="a-demo-app.html#fig-demo_controller_inheritance">Figure 2.17</a>.</p>
<div class="label" id="code-demo_users_controller_class"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 2.14.</span> <span class="description">The <code>UsersController</code> class, with inheritance. <br /> <code>app/controllers/users_controller.rb</code></span> </div>
<div class="code"><div class="highlight"><pre><span class="k">class</span> <span class="nc">UsersController</span> <span class="o"><</span> <span class="no">ApplicationController</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="k">end</span>
</pre></div>
</div></div>
<div class="label" id="code-demo_microposts_controller_class"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 2.15.</span> <span class="description">The <code>MicropostsController</code> class, with inheritance. <br /> <code>app/controllers/microposts_controller.rb</code></span> </div>
<div class="code"><div class="highlight"><pre><span class="k">class</span> <span class="nc">MicropostsController</span> <span class="o"><</span> <span class="no">ApplicationController</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="k">end</span>
</pre></div>
</div></div>
<div class="label" id="code-demo_application_controller_class"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 2.16.</span> <span class="description">The <code>ApplicationController</code> class, with inheritance. <br /> <code>app/controllers/application_controller.rb</code></span> </div>
<div class="code"><div class="highlight"><pre><span class="k">class</span> <span class="nc">ApplicationController</span> <span class="o"><</span> <span class="ss">ActionController::Base</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="k">end</span>
</pre></div>
</div></div>
<div class="label" id="fig-demo_controller_inheritance"></div>
<div class="figure"><div class="center"><span class="graphic"><img src="images/figures/demo_controller_inheritance.png" alt="demo_controller_inheritance" /></span></div><div class="caption"><span class="header">Figure 2.17: </span><span class="description">The inheritance hierarchy for the Users and Microposts controllers.</span></div></div>
<p>As with model inheritance, by inheriting ultimately from <code>ActionController::Base</code> both the Users and Microposts controllers gain a large amount of functionality, such as the ability to manipulate model objects, filter inbound HTTP requests, and render views as HTML. Since all Rails controllers inherit from <code>ApplicationController</code>, rules defined in the Application controller automatically apply to every action in the application. For example, in <a class="ref" href="sign-in-sign-out.html#sec-remember_me">Section 8.2.1</a> we’ll see how to include helpers for signing in and signing out of all of the sample application’s controllers.</p>
<div class="label" id="sec-deploying_the_demo_app"></div>
<h3><a id="sec-2_3_5" href="a-demo-app.html#sec-deploying_the_demo_app" class="heading"><span class="number">2.3.5</span> Deploying the demo app</a></h3>
<p>With the completion of the Microposts resource, now is a good time to push the repository up to GitHub:</p>
<div class="code"><div class="highlight"><pre><span class="gp">$</span> git add .
<span class="gp">$</span> git commit -m <span class="s2">"Finish demo app"</span>
<span class="gp">$</span> git push
</pre></div>
</div>
<p>Ordinarily, you should make smaller, more frequent commits, but for the purposes of this chapter a single big commit at the end is fine.</p>
<p>At this point, you can also deploy the demo app to Heroku as in <a class="ref" href="beginning.html#sec-deploying">Section 1.4</a>:</p>
<div class="code"><div class="highlight"><pre><span class="gp">$</span> heroku create