forked from mhartl/rails_tutorial_translation_2nd_ed
-
Notifications
You must be signed in to change notification settings - Fork 1
/
static-pages.html
2269 lines (1428 loc) · 201 KB
/
static-pages.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>static-pages</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></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-3" href="static-pages.html#top" class="heading"><span class="number">Chapter 3</span> Mostly static pages</a></h1>
<p>In this chapter, we will begin developing the sample application that will serve as our example throughout the rest of this tutorial. Although the sample app will eventually have users, microposts, and a full login and authentication framework, we will begin with a seemingly limited topic: the creation of static pages. Despite its apparent simplicity, making static pages is a highly instructive exercise, rich in implications—a perfect start for our nascent application.</p>
<p>Although Rails is designed for making database-backed dynamic websites, it also excels at making the kind of static pages we might make with raw HTML files. In fact, using Rails even for static pages yields a distinct advantage: we can easily add just a <em>small</em> amount of dynamic content. In this chapter we’ll learn how. Along the way, we’ll get our first taste of <em>automated testing</em>, which will help us be more confident that our code is correct. Moreover, having a good test suite will allow us to <em>refactor</em> our code with confidence, changing its form without changing its function.</p>
<p>There’s a lot of code in this chapter, especially in <a class="ref" href="static-pages.html#sec-first_tests">Section 3.2</a> and <a class="ref" href="static-pages.html#sec-slightly_dynamic_pages">Section 3.3</a>, and if you’re new to Ruby you shouldn’t worry about understanding the details right now. As noted in <a class="ref" href="beginning.html#sec-comments_for_various_readers">Section 1.1.1</a>, one strategy is to copy-and-paste the tests and use them to verify the application code, without worrying at this point how they work. In addition, <a class="ref" href="rails-flavored-ruby.html#top">Chapter 4</a> covers Ruby in more depth, so there is plenty of opportunity for these ideas to sink in. Finally, RSpec tests will recur throughout the tutorial, so if you get stuck now I recommend forging ahead; you’ll be amazed how, after just a few more chapters, initially inscrutable code will suddenly look simple.</p>
<p>As in <a class="ref" href="a-demo-app.html#top">Chapter 2</a>, before getting started we need to create a new Rails project, this time called <code>sample_app</code>:</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 sample_app --skip-test-unit
<span class="gp">$</span> <span class="nb">cd </span>sample_app
</pre></div>
</div>
<p>Here the <tt class="verb">--skip-test-unit</tt> option to the <code>rails</code> command tells Rails not to generate a <code>test</code> directory associated with the default <code>Test::Unit</code> framework. This is not because we won’t be writing tests; on the contrary, starting in <a class="ref" href="static-pages.html#sec-first_tests">Section 3.2</a> we will be using an alternate testing framework called <em>RSpec</em> to write a thorough test suite.</p>
<p>As in <a class="ref" href="a-demo-app.html#sec-planning_the_application">Section 2.1</a>, our next step is to use a text editor to update the <code>Gemfile</code> with the gems needed by our application. On the other hand, for the sample application we’ll also need two gems we didn’t need before: the gem for RSpec and the gem for the RSpec library specific to Rails. The code to include them is shown in <a class="ref" href="static-pages.html#code-gemfile_rspec">Listing 3.1</a>. (<em>Note</em>: If you would like to install <em>all</em> the gems needed for the sample application, you should use the code in <a class="ref" href="updating-showing-and-deleting-users.html#code-final_gemfile">Listing 9.49</a> at this time.)</p>
<div class="label" id="code-gemfile_rspec"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 3.1.</span> <span class="description">A <code>Gemfile</code> for the sample 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.13'</span>
<span class="n">group</span> <span class="ss">:development</span><span class="p">,</span> <span class="ss">:test</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="n">gem</span> <span class="s1">'rspec-rails'</span><span class="p">,</span> <span class="s1">'2.11.0'</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">:test</span> <span class="k">do</span>
<span class="n">gem</span> <span class="s1">'capybara'</span><span class="p">,</span> <span class="s1">'1.1.2'</span>
<span class="k">end</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>This includes <tt>rspec-rails</tt> in development mode so that we have access to RSpec-specific generators, and it includes it in test mode in order to run the tests. We don’t have to install RSpec itself because it is a dependency of <tt>rspec-rails</tt> and will thus be installed automatically. We also include the <a href="https://github.com/jnicklas/capybara">Capybara gem</a>, which allows us to simulate a user’s interaction with the sample application using a natural English-like syntax.<sup class="footnote" id="fnref-3_1"><a href="#fn-3_1">1</a></sup> As in <a class="ref" href="a-demo-app.html#top">Chapter 2</a>, we also must include the PostgreSQL gem in production for deployment to Heroku:</p>
<div class="code"><div class="highlight"><pre><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>
<p>Heroku recommends against using different databases in development and production, but for the sample application it won’t make any difference, and SQLite is <em>much</em> easier than PostgreSQL to install and configure. Installing and configuring PostgreSQL on your local machine is left as an exercise (<a class="ref" href="static-pages.html#sec-static_pages_exercises">Section 3.5</a>).</p>
<p>To install and include the new gems, we run <code>bundle update</code> and <code>bundle install</code>:</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>As in <a class="ref" href="beginning.html#sec-heroku_setup">Section 1.4.1</a> and <a class="ref" href="a-demo-app.html#top">Chapter 2</a>, we suppress the installation of production gems using the option <tt class="verb">--without</tt> <tt class="verb">production</tt>. This is a “remembered option”, which means that we don’t have to include it in future invocations of Bundler. Instead, we can write simply <code>bundle install</code> and production gems will be ignored automatically.<sup class="footnote" id="fnref-3_2"><a href="#fn-3_2">2</a></sup></p>
<p>Next, we need to configure Rails to use RSpec in place of <code>Test::Unit</code>. This can be accomplished with <code>rails generate rspec:install</code>:</p>
<div class="code"><div class="highlight"><pre><span class="gp">$</span> rails generate rspec:install
</pre></div>
</div>
<p>If your system complains about the lack of a JavaScript runtime, visit the <a href="https://github.com/sstephenson/execjs">execjs page at GitHub</a> for a list of possibilities. I particularly recommend installing <a href="http://nodejs.org/">Node.js</a>.</p>
<p>With that, all we have left is to initialize the Git repository:<sup class="footnote" id="fnref-3_3"><a href="#fn-3_3">3</a></sup></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>
<p>As with the first application, I suggest updating the <code>README</code> file (located in the root directory of the application) to be more helpful and descriptive, as shown in <a class="ref" href="static-pages.html#code-sample_app_readme">Listing 3.2</a>.</p>
<div class="label" id="code-sample_app_readme"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 3.2.</span> <span class="description">An improved <code>README</code> file for the sample app.</span> </div>
<div class="code"><div class="highlight"><pre># Ruby on Rails Tutorial: sample application
This is the sample application for
[*Ruby on Rails Tutorial: Learn Rails by Example*](http://railstutorial.org/)
by [Michael Hartl](http://michaelhartl.com/).
</pre></div>
</div></div>
<p>Then change it to use the Markdown extension <code>.md</code> and commit the changes:</p>
<div class="code"><div class="highlight"><pre><span class="gp">$</span> git mv README.rdoc README.md
<span class="gp">$</span> git commit -a -m <span class="s2">"Improve the README"</span>
</pre></div>
</div>
<div class="label" id="fig-create_repository"></div>
<div class="figure"><div class="center"><span class="graphic"><img src="images/figures/create_repository_new.png" alt="create_repository_new" /></span></div><div class="caption"><span class="header">Figure 3.1: </span><span class="description">Creating the sample app repository at GitHub. <a href="http://railstutorial.org/images/figures/create_repository_new-full.png">(full size)</a></span></div></div>
<p>Since we’ll be using this sample app throughout the rest of the book, it’s a good idea to make a repository at GitHub (<a class="ref" href="static-pages.html#fig-create_repository">Figure 3.1</a>) and push it up:</p>
<div class="code"><div class="highlight"><pre><span class="gp">$</span> git remote add origin git@github.com:<username>/sample_app.git
<span class="gp">$</span> git push -u origin master
</pre></div>
</div>
<p>As a result of my performing this step, you can find the <a href="https://github.com/railstutorial/sample_app_2nd_ed">Rails Tutorial sample application code on GitHub</a> (under a slightly different name).<sup class="footnote" id="fnref-3_4"><a href="#fn-3_4">4</a></sup></p>
<p>Of course, we can optionally deploy the app to Heroku even at this early stage:</p>
<div class="code"><div class="highlight"><pre><span class="gp">$</span> heroku create --stack cedar
<span class="gp">$</span> git push heroku master
</pre></div>
</div>
<p>As you proceed through the rest of the book, I recommend pushing and deploying the application regularly:</p>
<div class="code"><div class="highlight"><pre><span class="gp">$</span> git push
<span class="gp">$</span> git push heroku
</pre></div>
</div>
<p>This provides remote backups and lets you catch any production errors as soon as possible. If you run into problems at Heroku, make sure to take a look at the production logs to try to diagnose the problem:</p>
<div class="code"><div class="highlight"><pre><span class="gp">$</span> heroku logs
</pre></div>
</div>
<p>With all the preparation finished, we’re finally ready to get started developing the sample application.</p>
<div class="label" id="sec-static_pages"></div>
<h2><a id="sec-3_1" href="static-pages.html#sec-static_pages" class="heading"><span class="number">3.1</span> Static pages</a></h2>
<p>Rails has two main ways of making static web pages. First, Rails can handle <em>truly</em> static pages consisting of raw HTML files. Second, Rails allows us to define <em>views</em> containing raw HTML, which Rails can <em>render</em> so that the web server can send it to the browser.</p>
<p>In order to get our bearings, it’s helpful to recall the Rails directory structure from <a class="ref" href="beginning.html#sec-the_first_application">Section 1.2.3</a> (<a class="ref" href="beginning.html#fig-directory_structure_rails_3_2">Figure 1.2</a>). In this section, we’ll be working mainly in the <code>app/controllers</code> and <code>app/views</code> directories. (In <a class="ref" href="static-pages.html#sec-first_tests">Section 3.2</a>, we’ll even add a new directory of our own.)</p>
<p>This is the first section where it’s useful to be able to open the entire Rails directory in your text editor or IDE. Unfortunately, how to do this is system-dependent, but in many cases you can open the current application directory, represented in Unix by a dot <code>.</code>, using the command-line command for your editor of choice:</p>
<div class="code"><div class="highlight"><pre><span class="gp">$</span> <span class="nb">cd</span> ~/rails_projects/sample_app
<span class="gp">$</span> <editor name> .
</pre></div>
</div>
<p>For example, to open the sample app in Sublime Text, you type</p>
<div class="code"><div class="highlight"><pre><span class="gp">$</span> subl .
</pre></div>
</div>
<p>For Vim, you type <code>vim .</code>, <code>gvim .</code>, or <code>mvim .</code> depending on which flavor of Vim you use.</p>
<div class="label" id="sec-truly_static_pages"></div>
<h3><a id="sec-3_1_1" href="static-pages.html#sec-truly_static_pages" class="heading"><span class="number">3.1.1</span> Truly static pages</a></h3>
<p>We start with truly static pages. Recall from <a class="ref" href="beginning.html#sec-rails_server">Section 1.2.5</a> that every Rails application comes with a minimal working application thanks to the <code>rails</code> script, with a default welcome page at the address <a href="http://localhost:3000/">http://localhost:3000/</a> (<a class="ref" href="beginning.html#fig-riding_rails_31">Figure 1.3</a>).</p>
<div class="label" id="fig-public_index_rails_3"></div>
<div class="figure"><div class="center"><span class="graphic"><img src="images/figures/public_index_rails_3.png" alt="public_index_rails_3" /></span></div><div class="caption"><span class="header">Figure 3.2: </span><span class="description">The <code>public/index.html</code> file. <a href="http://railstutorial.org/images/figures/public_index_rails_3-full.png">(full size)</a></span></div></div>
<p>To learn where this page comes from, take a look at the file <code>public/index.html</code> (<a class="ref" href="static-pages.html#fig-public_index_rails_3">Figure 3.2</a>). Because the file contains its own stylesheet information, it’s a little messy, but it gets the job done: by default, Rails serves any files in the <code>public</code> directory directly to the browser.<sup class="footnote" id="fnref-3_5"><a href="#fn-3_5">5</a></sup> In the case of the special <code>index.html</code> file, you don’t even have to indicate the file in the URI, as <code>index.html</code> is the default. You can include it if you want, though; the addresses http://localhost:3000/ http://localhost:3000/index.html are equivalent.</p>
<p>As you might expect, if we want we can make our own static HTML files and put them in the same <code>public</code> directory as <code>index.html</code>. For example, let’s create a file with a friendly greeting (<a class="ref" href="static-pages.html#code-hello_world">Listing 3.3</a>):<sup class="footnote" id="fnref-3_6"><a href="#fn-3_6">6</a></sup></p>
<div class="code"><div class="highlight"><pre><span class="gp">$</span> subl public/hello.html
</pre></div>
</div>
<div class="label" id="code-hello_world"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 3.3.</span> <span class="description">A typical HTML file, with a friendly greeting. <br /> <code>public/hello.html</code></span> </div>
<div class="code"><div class="highlight"><pre><span class="cp"><!DOCTYPE html></span>
<span class="nt"><html></span>
<span class="nt"><head></span>
<span class="nt"><title></span>Greeting<span class="nt"></title></span>
<span class="nt"></head></span>
<span class="nt"><body></span>
<span class="nt"><p></span>Hello, world!<span class="nt"></p></span>
<span class="nt"></body></span>
<span class="nt"></html></span>
</pre></div>
</div></div>
<p>We see in <a class="ref" href="static-pages.html#code-hello_world">Listing 3.3</a> the typical structure of an HTML document: a <em>document type</em>, or doctype, declaration at the top to tell browsers which version of HTML we’re using (in this case, <a href="http://en.wikipedia.org/wiki/HTML5">HTML5</a>);<sup class="footnote" id="fnref-3_7"><a href="#fn-3_7">7</a></sup> a <code>head</code> section, in this case with “Greeting” inside a <code>title</code> tag; and a <code>body</code> section, in this case with “Hello, world!” inside a <code>p</code> (paragraph) tag. (The indentation is optional—HTML is not sensitive to whitespace, and ignores both tabs and spaces—but it makes the document’s structure easier to see.)</p>
<p>Now run a local server using</p>
<div class="code"><div class="highlight"><pre><span class="gp">$</span> rails server
</pre></div>
</div>
<p>and navigate to <a href="http://localhost:3000/hello.html">http://localhost:3000/hello.html</a>. As promised, Rails renders the page straightaway (<a class="ref" href="static-pages.html#fig-hello_world">Figure 3.3</a>). Note that the title displayed at the top of the browser window in <a class="ref" href="static-pages.html#fig-hello_world">Figure 3.3</a> is just the contents inside the <code>title</code> tag, namely, “Greeting”.</p>
<div class="label" id="fig-hello_world"></div>
<div class="figure"><div class="center"><span class="graphic"><img src="images/figures/hello_world.png" alt="hello_world" /></span></div><div class="caption"><span class="header">Figure 3.3: </span><span class="description">A new static HTML file. <a href="http://railstutorial.org/images/figures/hello_world-full.png">(full size)</a></span></div></div>
<p>Since this file is just for demonstration purposes, we don’t really want it to be part of our sample application, so it’s probably best to remove it once the thrill of creating it has worn off:</p>
<div class="code"><div class="highlight"><pre><span class="gp">$</span> rm public/hello.html
</pre></div>
</div>
<p>We’ll leave the <code>index.html</code> file alone for now, but of course eventually we should remove it: we don’t want the root of our application to be the Rails default page shown in <a class="ref" href="beginning.html#fig-riding_rails_31">Figure 1.3</a>. We’ll see in <a class="ref" href="filling-in-the-layout.html#sec-layout_links">Section 5.3</a> how to change the address <a href="http://localhost:3000/">http://localhost:3000/</a> to point to something other than <code>public/index.html</code>.</p>
<div class="label" id="sec-static_pages_with_rails"></div>
<h3><a id="sec-3_1_2" href="static-pages.html#sec-static_pages_with_rails" class="heading"><span class="number">3.1.2</span> Static pages with Rails</a></h3>
<p>The ability to return static HTML files is nice, but it’s not particularly useful for making dynamic web applications. In this section, we’ll take a first step toward making dynamic pages by creating a set of Rails <em>actions</em>, which are a more powerful way to define URIs than static files.<sup class="footnote" id="fnref-3_8"><a href="#fn-3_8">8</a></sup> Rails actions come bundled together inside <em>controllers</em> (the C in MVC from <a class="ref" href="beginning.html#sec-mvc">Section 1.2.6</a>), which contain sets of actions related by a common purpose. We got a glimpse of controllers in <a class="ref" href="a-demo-app.html#top">Chapter 2</a>, and will come to a deeper understanding once we explore the <a href="http://en.wikipedia.org/wiki/Representational_State_Transfer">REST architecture</a> more fully (starting in <a class="ref" href="modeling-users.html#top">Chapter 6</a>); in essence, a controller is a container for a group of (possibly dynamic) web pages.</p>
<p>To get started, recall from <a class="ref" href="beginning.html#sec-git_commands">Section 1.3.5</a> that, when using Git, it’s a good practice to do our work on a separate topic branch rather than the master branch. If you’re using Git for version control, you should run the following command:</p>
<div class="code"><div class="highlight"><pre><span class="gp">$</span> git checkout -b static-pages
</pre></div>
</div>
<p>Rails comes with a script for making controllers called <code>generate</code>; all it needs to work its magic is the controller’s name. In order to use <code>generate</code> with RSpec, you need to run the RSpec generator command if you didn’t run it when following the introduction to this chapter:</p>
<div class="code"><div class="highlight"><pre><span class="gp">$</span> rails generate rspec:install
</pre></div>
</div>
<p>Since we’ll be making a controller to handle static pages, we’ll call it the StaticPages controller. We’ll also plan to make actions for a Home page, a Help page, and an About page. The <code>generate</code> script takes an optional list of actions, so we’ll include two of the initial actions directly on the command line (<a class="ref" href="static-pages.html#code-generating_pages">Listing 3.4</a>).</p>
<div class="label" id="code-generating_pages"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 3.4.</span> <span class="description">Generating a StaticPages controller.</span> </div>
<div class="code"><div class="highlight"><pre>$ rails generate controller StaticPages home help --no-test-framework
create app/controllers/static_pages_controller.rb
route get "static_pages/help"
route get "static_pages/home"
invoke erb
create app/views/static_pages
create app/views/static_pages/home.html.erb
create app/views/static_pages/help.html.erb
invoke helper
create app/helpers/static_pages_helper.rb
invoke assets
invoke coffee
create app/assets/javascripts/static_pages.js.coffee
invoke scss
create app/assets/stylesheets/static_pages.css.scss
</pre></div>
</div></div>
<p>Here we’ve used the option <tt class="verb">--no-test-framework</tt> to suppress the generation of the default RSpec tests, which we won’t be using. Instead, we’ll create the tests by hand starting in <a class="ref" href="static-pages.html#sec-first_tests">Section 3.2</a>. We’ve also intentionally left off the <code>about</code> action from the command line arguments in <a class="ref" href="static-pages.html#code-generating_pages">Listing 3.4</a> so that we can see how to add it using test-driven development, or TDD (<a class="ref" href="static-pages.html#sec-first_tests">Section 3.2</a>).</p>
<p>In <a class="ref" href="static-pages.html#code-generating_pages">Listing 3.4</a>, note that we have passed the controller name as so-called <a href="https://en.wikipedia.org/wiki/CamelCase">CamelCase</a>, which leads to the creation of a controller file written in <a href="https://en.wikipedia.org/wiki/Snake_case">snake case</a>, so that a controller called StaticPages yields a file called <code>static_pages_controller.rb</code>. This is merely a convention, and in fact using snake case at the command line also works: the command</p>
<div class="code"><div class="highlight"><pre><span class="gp">$</span> rails generate controller static_pages ...
</pre></div>
</div>
<p>also generates a controller called <code>static_pages_controller.rb</code>. Because Ruby uses CamelCase for class names (<a class="ref" href="rails-flavored-ruby.html#sec-ruby_classes">Section 4.4</a>), my preference is to refer to controllers using their CamelCase names, but this is a matter of taste. (Since Ruby filenames typically use snake case, the Rails generator converts CamelCase to snake case using the <a href="http://api.rubyonrails.org/classes/ActiveSupport/Inflector.html#method-i-underscore"><tt>underscore</tt></a> method.)</p>
<p>By the way, if you ever make a mistake when generating code, it’s useful to know how to reverse the process. See <a class="ref" href="static-pages.html#sidebar-undoing_things">Box 3.1</a> for some techniques on how to undo things in Rails.</p>
<div class="label" id="sidebar-undoing_things"></div>
<div class="sidebar"><span class="title"><span class="header">Box 3.1.</span><span class="description">Undoing things</span></span>
<p>Even when you’re very careful, things can sometimes go wrong when developing Rails applications. Happily, Rails has some facilities to help you recover.</p>
<p>One common scenario is wanting to undo code generation—for example, if you change your mind on the name of a controller. When generating a controller, Rails creates many more files than the controller file itself (as seen in <a class="ref" href="static-pages.html#code-generating_pages">Listing 3.4</a>). Undoing the generation means removing not only the principal generated file, but all the ancillary files as well. (In fact, we also want to undo any automatic edits made to the <tt>routes.rb</tt> file.) In Rails, this can be accomplished with <tt>rails destroy</tt>. In particular, these two commands cancel each other out:</p>
<pre class="verbatim"> $ rails generate controller FooBars baz quux
$ rails destroy controller FooBars baz quux</pre>
<p>Similarly, in <a class="ref" href="modeling-users.html#top">Chapter 6</a> we’ll generate a <em>model</em> as follows:</p>
<pre class="verbatim"> $ rails generate model Foo bar:string baz:integer</pre>
<p>This can be undone using</p>
<pre class="verbatim"> $ rails destroy model Foo</pre>
<p>(In this case, it turns out we can omit the other command-line arguments. When you get to <a class="ref" href="modeling-users.html#top">Chapter 6</a>, see if you can figure out why.)</p>
<p>Another technique related to models involves undoing <em>migrations</em>, which we saw briefly in <a class="ref" href="a-demo-app.html#top">Chapter 2</a> and will see much more of starting in <a class="ref" href="modeling-users.html#top">Chapter 6</a>. Migrations change the state of the database using</p>
<pre class="verbatim"> $ rake db:migrate</pre>
<p>We can undo a single migration step using</p>
<pre class="verbatim"> $ rake db:rollback</pre>
<p>To go all the way back to the beginning, we can use</p>
<pre class="verbatim"> $ rake db:migrate VERSION=0</pre>
<p>As you might guess, substituting any other number for <tt>0</tt> migrates to that version number, where the version numbers come from listing the migrations sequentially.</p>
<p>With these techniques in hand, we are well-equipped to recover from the inevitable development <a href="http://en.wikipedia.org/wiki/SNAFU">snafus</a>.</p>
</div>
<p>The StaticPages controller generation in <a class="ref" href="static-pages.html#code-generating_pages">Listing 3.4</a> automatically updates the <em>routes</em> file, called <code>config/routes.rb</code>, which Rails uses to find the correspondence between URIs and web pages. This is our first encounter with the <code>config</code> directory, so it’s helpful to take a quick look at it (<a class="ref" href="static-pages.html#fig-config_directory_rails_3">Figure 3.4</a>). The <code>config</code> directory is where Rails collects files needed for the application configuration—hence the name.</p>
<div class="label" id="fig-config_directory_rails_3"></div>
<div class="figure"><div class="center"><span class="graphic"><img src="images/figures/config_directory_rails_3.png" alt="config_directory_rails_3" /></span></div><div class="caption"><span class="header">Figure 3.4: </span><span class="description">Contents of the sample app’s <code>config</code> directory. <a href="http://railstutorial.org/images/figures/config_directory_rails_3-full.png">(full size)</a></span></div></div>
<p>Since we generated <code>home</code> and <code>help</code> actions, the routes file already has a rule for each one, as seen in <a class="ref" href="static-pages.html#code-pages_routes">Listing 3.5</a>.</p>
<div class="label" id="code-pages_routes"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 3.5.</span> <span class="description">The routes for the <code>home</code> and <code>help</code> actions in the StaticPages controller. <br /> <code>config/routes.rb</code></span> </div>
<div class="code"><div class="highlight"><pre><span class="ss">SampleApp::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">get</span> <span class="s2">"static_pages/home"</span>
<span class="n">get</span> <span class="s2">"static_pages/help"</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="k">end</span>
</pre></div>
</div></div>
<p>Here the rule</p>
<div class="code"><div class="highlight"><pre><span class="n">get</span> <span class="s2">"static_pages/home"</span>
</pre></div>
</div>
<p>maps requests for the URI /static_pages/home to the <code>home</code> action in the StaticPages controller. Moreover, by using <code>get</code> we arrange for the route to respond to a <tt>GET</tt> request, which is one of the fundamental
<em>HTTP verbs</em> supported by the hypertext transfer protocol (<a class="ref" href="static-pages.html#sidebar-get_etc">Box 3.2</a>). In our case, this means that when we generate a <code>home</code> action inside the StaticPages controller we automatically get a page at the address /static_pages/home. To see the results, navigate to <a href="http://localhost:3000/static_pages/home">/static_pages/home</a> (<a class="ref" href="static-pages.html#fig-raw_home_view">Figure 3.5</a>).</p>
<div class="label" id="fig-raw_home_view"></div>
<div class="figure"><div class="center"><span class="graphic"><img src="images/figures/raw_home_view_31.png" alt="raw_home_view_31" /></span></div><div class="caption"><span class="header">Figure 3.5: </span><span class="description">The raw home view (<a href="http://localhost:3000/static_pages/home">/static_pages/home</a>). <a href="http://railstutorial.org/images/figures/raw_home_view_31-full.png">(full size)</a></span></div></div>
<div class="label" id="sidebar-get_etc"></div>
<div class="sidebar"><span class="title"><span class="header">Box 3.2.</span><span class="description"><tt>GET</tt>, et cet.</span></span>
<p>The hypertext transfer protocol (<a href="http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods">HTTP</a>) defines four basic operations, corresponding to the four verbs <em>get</em>, <em>post</em>, <em>put</em>, and <em>delete</em>. These refer to operations between a <em>client</em> computer (typically running a web browser such as Firefox or Safari) and a <em>server</em> (typically running a web server such as Apache or Nginx). (It’s important to understand that, when developing Rails applications on a local computer, the client and server are the same physical machine, but in general they are different.) An emphasis on HTTP verbs is typical of web frameworks (including Rails) influenced by the <em>REST architecture</em>, which we saw briefly in <a class="ref" href="a-demo-app.html#top">Chapter 2</a> and will start learning about more in <a class="ref" href="sign-up.html#top">Chapter 7</a>.</p>
<p><tt>GET</tt> is the most common HTTP operation, used for <em>reading</em> data on the web; it just means “get a page”, and every time you visit a site like google.com or wikipedia.org your browser is submitting a <tt>GET</tt> request. <tt>POST</tt> is the next most common operation; it is the request sent by your browser when you submit a form. In Rails applications, <tt>POST</tt> requests are typically used for <em>creating</em> things (although HTTP also allows <tt>POST</tt> to perform updates); for example, the <tt>POST</tt> request sent when you submit a registration form creates a new user on the remote site. The other two verbs, <tt>PUT</tt> and <tt>DELETE</tt>, are designed for <em>updating</em> and <em>destroying</em> things on the remote server. These requests are less common than <tt>GET</tt> and <tt>POST</tt> since browsers are incapable of sending them natively, but some web frameworks (including Ruby on Rails) have clever ways of making it <em>seem</em> like browsers are issuing such requests.</p>
</div>
<p>To understand where this page comes from, let’s start by taking a look at the StaticPages controller in a text editor; you should see something like <a class="ref" href="static-pages.html#code-static_pages_controller">Listing 3.6</a>. You may note that, unlike the demo Users and Microposts controllers from <a class="ref" href="a-demo-app.html#top">Chapter 2</a>, the StaticPages controller does not use the standard REST actions. This is normal for a collection of static pages—the REST architecture isn’t the best solution to every problem.</p>
<div class="label" id="code-static_pages_controller"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 3.6.</span> <span class="description">The StaticPages controller made by <a class="ref" href="static-pages.html#code-generating_pages">Listing 3.4</a>. <br /> <code>app/controllers/static_pages_controller.rb</code></span> </div>
<div class="code"><div class="highlight"><pre><span class="k">class</span> <span class="nc">StaticPagesController</span> <span class="o"><</span> <span class="no">ApplicationController</span>
<span class="k">def</span> <span class="nf">home</span>
<span class="k">end</span>
<span class="k">def</span> <span class="nf">help</span>
<span class="k">end</span>
<span class="k">end</span>
</pre></div>
</div></div>
<p>We see from the <code>class</code> keyword in <a class="ref" href="static-pages.html#code-static_pages_controller">Listing 3.6</a> that <code>static_pages_controller.rb</code> defines a <em>class</em>, in this case called <code>StaticPagesController</code>. Classes are simply a convenient way to organize <em>functions</em> (also called <em>methods</em>) like the <code>home</code> and <code>help</code> actions, which are defined using the <code>def</code> keyword. The angle bracket <code><</code> indicates that <code>StaticPagesController</code> <em>inherits</em> from the Rails class <code>ApplicationController</code>; as we’ll see momentarily, this means that our pages come equipped with a large amount of Rails-specific functionality. (We’ll learn more about both classes and inheritance in <a class="ref" href="rails-flavored-ruby.html#sec-ruby_classes">Section 4.4</a>.)</p>
<p>In the case of the StaticPages controller, both its methods are initially empty:</p>
<div class="code"><div class="highlight"><pre><span class="k">def</span> <span class="nf">home</span>
<span class="k">end</span>
<span class="k">def</span> <span class="nf">help</span>
<span class="k">end</span>
</pre></div>
</div>
<p>In plain Ruby, these methods would simply do nothing. In Rails, the situation is different; <code>StaticPagesController</code> is a Ruby class, but because it inherits from <code>ApplicationController</code> the behavior of its methods is specific to Rails: when visiting the URI /static_pages/home, Rails looks in the StaticPages controller and executes the code in the <code>home</code> action, and then renders the <em>view</em> (the V in MVC from <a class="ref" href="beginning.html#sec-mvc">Section 1.2.6</a>) corresponding to the action. In the present case, the <code>home</code> action is empty, so all visiting /static_pages/home does is render the view. So, what does a view look like, and how do we find it?</p>
<p>If you take another look at the output in <a class="ref" href="static-pages.html#code-generating_pages">Listing 3.4</a>, you might be able to guess the correspondence between actions and views: an action like <code>home</code> has a corresponding view called <code>home.html.erb</code>. We’ll learn in <a class="ref" href="static-pages.html#sec-slightly_dynamic_pages">Section 3.3</a> what the <code>.erb</code> part means; from the <code>.html</code> part you probably won’t be surprised that it basically looks like HTML (<a class="ref" href="static-pages.html#code-raw_home_view">Listing 3.7</a>).</p>
<div class="label" id="code-raw_home_view"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 3.7.</span> <span class="description">The generated view for the Home page. <br /> <code>app/views/static_pages/home.html.erb</code></span> </div>
<div class="code"><div class="highlight"><pre><span class="nt"><h1></span>StaticPages#home<span class="nt"></h1></span>
<span class="nt"><p></span>Find me in app/views/static_pages/home.html.erb<span class="nt"></p></span>
</pre></div>
</div></div>
<p>The view for the <code>help</code> action is analogous (<a class="ref" href="static-pages.html#code-raw_help_view">Listing 3.8</a>).</p>
<div class="label" id="code-raw_help_view"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 3.8.</span> <span class="description">The generated view for the Help page. <br /> <code>app/views/static_pages/help.html.erb</code></span> </div>
<div class="code"><div class="highlight"><pre><span class="nt"><h1></span>StaticPages#help<span class="nt"></h1></span>
<span class="nt"><p></span>Find me in app/views/static_pages/help.html.erb<span class="nt"></p></span>
</pre></div>
</div></div>
<p>Both of these views are just placeholders: they have a top-level heading (inside the <code>h1</code> tag) and a paragraph (<code>p</code> tag) with the full path to the relevant file. We’ll add some (very slightly) dynamic content starting in <a class="ref" href="static-pages.html#sec-slightly_dynamic_pages">Section 3.3</a>, but as they stand these views underscore an important point: Rails views can simply contain static HTML. As far as the browser is concerned, the raw HTML files from <a class="ref" href="static-pages.html#sec-truly_static_pages">Section 3.1.1</a> and the controller/action method of delivering pages are indistinguishable: all the browser ever sees is HTML.</p>
<p>In the remainder of this chapter, we’ll add some custom content to the Home and Help pages, and then add in the About page we left off in <a class="ref" href="static-pages.html#sec-static_pages_with_rails">Section 3.1.2</a>. Then we’ll add a very small amount of dynamic content by changing the title on a per-page basis.</p>
<p>Before moving on, if you’re using Git it’s a good idea to add the files for the StaticPages controller to the repository:</p>
<div class="code"><div class="highlight"><pre><span class="gp">$</span> git add .
<span class="gp">$</span> git commit -m <span class="s2">"Add a StaticPages controller"</span>
</pre></div>
</div>
<div class="label" id="sec-first_tests"></div>
<h2><a id="sec-3_2" href="static-pages.html#sec-first_tests" class="heading"><span class="number">3.2</span> Our first tests</a></h2>
<p>The <em>Rails Tutorial</em> takes an intuitive approach to testing that emphasizes the behavior of the application rather than its precise implementation, a variant of test-driven development (TDD) known as behavior-driven development (BDD). Our main tools will be <em>integration tests</em> (starting in this section) and <em>unit tests</em> (starting in <a class="ref" href="modeling-users.html#top">Chapter 6</a>). Integration tests, known as <em>request specs</em> in the context of RSpec, allow us to simulate the actions of a user interacting with our application using a web browser. Together with the natural-language syntax provided by Capybara, integration tests provide a powerful method to test our application’s functionality without having to manually check each page with a browser. (Another popular choice for BDD, called Cucumber, is introduced in <a class="ref" href="sign-in-sign-out.html#sec-cucumber">Section 8.3</a>.)</p>
<p>The defining quality of TDD is writing tests <em>first</em>, before the application code. Initially, this might take some getting used to, but the benefits are significant. By writing a <em>failing</em> test first and then implementing the application code to get it to pass, we increase our confidence that the test is actually covering the functionality we think it is. Moreover, the fail-implement-pass development cycle induces a <a href="http://en.wikipedia.org/wiki/Flow_(psychology)">flow state</a>, leading to enjoyable coding and high productivity. Finally, the tests act as a <em>client</em> for the application code, often leading to more elegant software designs.</p>
<p>It’s important to understand that TDD is not always the right tool for the job: there’s no reason to dogmatically insist that tests always should be written first, that they should cover every single feature, or that there should necessarily be any tests at all. For example, when you aren’t at all sure how to solve a given programming problem, it’s often useful to skip the tests and write only application code, just to get a sense of what the solution will look like. (In the language of <a href="http://en.wikipedia.org/wiki/Extreme_Programming">Extreme Programming (XP)</a>, this exploratory step is called a <em>spike</em>.) Once you see the general shape of the solution, you can then use TDD to implement a more polished version.</p>
<p>In this section, we’ll be running the tests using the <code>rspec</code> command supplied by the RSpec gem. This practice is straightforward but not ideal, and if you are a more advanced user I suggest setting up your system as described in <a class="ref" href="static-pages.html#sec-advanced_setup">Section 3.6</a>.</p>
<div class="label" id="sec-TDD"></div>
<h3><a id="sec-3_2_1" href="static-pages.html#sec-TDD" class="heading"><span class="number">3.2.1</span> Test-driven development</a></h3>
<p>In test-driven development, we first write a <em>failing</em> test, represented in many testing tools by the color red. We then implement code to get the test to pass, represented by the color green. Finally, if necessary, we refactor the code, changing its form (by eliminating duplication, for example) without changing its function. This cycle is known as “Red, Green, Refactor”.</p>
<p>We’ll begin by adding some content to the Home page using test-driven development, including a top-level heading (<code><h1></code>) with the content <code>Sample App</code>. The first step is to generate an integration test (request spec) for our static pages:</p>
<div class="code"><div class="highlight"><pre><span class="gp">$</span> rails generate integration_test static_pages
<span class="go"> invoke rspec</span>
<span class="go"> create spec/requests/static_pages_spec.rb</span>
</pre></div>
</div>
<p>This creates the <code>static_pages_spec.rb</code> in the <code>spec/requests</code> directory. As with most generated code, the result is not pretty, so let’s open <code>static_pages_spec.rb</code> with a text editor and replace it with the contents of <a class="ref" href="static-pages.html#code-home_page_content_spec">Listing 3.9</a>.</p>
<div class="label" id="code-home_page_content_spec"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 3.9.</span> <span class="description">Code to test the contents of the Home page. <br /> <code>spec/requests/static_pages_spec.rb</code></span> </div>
<div class="code"><div class="highlight"><pre><span class="nb">require</span> <span class="s1">'spec_helper'</span>
<span class="n">describe</span> <span class="s2">"Static pages"</span> <span class="k">do</span>
<span class="n">describe</span> <span class="s2">"Home page"</span> <span class="k">do</span>
<span class="n">it</span> <span class="s2">"should have the content 'Sample App'"</span> <span class="k">do</span>
<span class="n">visit</span> <span class="s1">'/static_pages/home'</span>
<span class="n">page</span><span class="o">.</span><span class="n">should</span> <span class="n">have_content</span><span class="p">(</span><span class="s1">'Sample App'</span><span class="p">)</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">end</span>
</pre></div>
</div></div>
<p>The code in <a class="ref" href="static-pages.html#code-home_page_content_spec">Listing 3.9</a> is pure Ruby, but even if you’ve studied Ruby before it probably won’t look very familiar. This is because RSpec uses the general malleability of Ruby to define a <em>domain-specific language</em> (DSL) built just for testing. The important point is that <em>you do not need to understand RSpec’s syntax to be able to use RSpec</em>. It may seem like magic at first, but RSpec and Capybara are designed to read more or less like English, and if you follow the examples from the <code>generate</code> script and the other examples in this tutorial you’ll pick it up fairly quickly.</p>
<p><a class="ref" href="static-pages.html#code-home_page_content_spec">Listing 3.9</a> contains a <code>describe</code> block with one <em>example</em>, i.e., a block starting with <code>it "…" do</code>:</p>
<div class="code"><div class="highlight"><pre><span class="n">describe</span> <span class="s2">"Home page"</span> <span class="k">do</span>
<span class="n">it</span> <span class="s2">"should have the content 'Sample App'"</span> <span class="k">do</span>
<span class="n">visit</span> <span class="s1">'/static_pages/home'</span>
<span class="n">page</span><span class="o">.</span><span class="n">should</span> <span class="n">have_content</span><span class="p">(</span><span class="s1">'Sample App'</span><span class="p">)</span>
<span class="k">end</span>
<span class="k">end</span>
</pre></div>
</div>
<p>The first line indicates that we are describing the Home page. This is just a string, and it can be anything you want; RSpec doesn’t care, but you and other human readers probably do. Then the spec says that when you visit the Home page at <code>/static_pages/home</code>, the content should contain the words “Sample App”. As with the first line, what goes inside the quote marks is irrelevant to RSpec, and is intended to be descriptive to human readers. Then the line</p>
<div class="code"><div class="highlight"><pre><span class="n">visit</span> <span class="s1">'/static_pages/home'</span>
</pre></div>
</div>
<p>uses the Capybara function <code>visit</code> to simulate visiting the URI <code>/static_pages/home</code> in a browser, while the line</p>
<div class="code"><div class="highlight"><pre><span class="n">page</span><span class="o">.</span><span class="n">should</span> <span class="n">have_content</span><span class="p">(</span><span class="s1">'Sample App'</span><span class="p">)</span>
</pre></div>
</div>
<p>uses the <code>page</code> variable (also provided by Capybara) to test that the resulting page has the right content.</p>
<p>To run the test, we have several options, including some convenient but rather advanced tools discussed in <a class="ref" href="static-pages.html#sec-advanced_setup">Section 3.6</a>. For now, we’ll use the <code>rspec</code> command at the command line (executed with <code>bundle exec</code> to ensure that RSpec runs in the environment specified by our <code>Gemfile</code>):<sup class="footnote" id="fnref-3_9"><a href="#fn-3_9">9</a></sup></p>
<div class="code"><div class="highlight"><pre><span class="gp">$</span> bundle <span class="nb">exec </span>rspec spec/requests/static_pages_spec.rb
</pre></div>
</div>
<p>This yields a failing test. The appearance of the result depends on your system; on my system, the red failing test appears as in <a class="ref" href="static-pages.html#fig-red_failing_spec">Figure 3.6</a>.<sup class="footnote" id="fnref-3_10"><a href="#fn-3_10">10</a></sup> (The screenshot, which predates, the current Git branching strategy, shows work on the <tt>master</tt> branch instead the <tt>static-pages</tt> branch, but this is not cause for concern.)</p>
<div class="label" id="fig-red_failing_spec"></div>
<div class="figure"><div class="center"><span class="graphic"><img src="images/figures/red_failing_spec.png" alt="red_failing_spec" /></span></div><div class="caption"><span class="header">Figure 3.6: </span><span class="description">A red (failing) test. <a href="http://railstutorial.org/images/figures/red_failing_spec-full.png">(full size)</a></span></div></div>
<p>To get the test to pass, we’ll replace the default Home page test with the HTML in <a class="ref" href="static-pages.html#code-home_page_passing">Listing 3.10</a>.</p>
<div class="label" id="code-home_page_passing"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 3.10.</span> <span class="description">Code to get a passing test for the Home page. <br /> <code>app/views/static_pages/home.html.erb</code></span> </div>
<div class="code"><div class="highlight"><pre><span class="nt"><h1></span>Sample App<span class="nt"></h1></span>
<span class="nt"><p></span>
This is the home page for the
<span class="nt"><a</span> <span class="na">href=</span><span class="s">"http://railstutorial.org/"</span><span class="nt">></span>Ruby on Rails Tutorial<span class="nt"></a></span>
sample application.
<span class="nt"></p></span>
</pre></div>
</div></div>
<p>This arranges for a top-level heading (<code><h1></code>) with the content <code>Sample App</code>, which should get the test to pass. We also include an <em>anchor</em> tag <code>a</code>, which creates links to the given URI (called an “href”, or “hypertext reference”, in the context of an anchor tag):</p>
<div class="code"><div class="highlight"><pre><span class="nt"><a</span> <span class="na">href=</span><span class="s">"http://railstutorial.org/"</span><span class="nt">></span>Ruby on Rails Tutorial<span class="nt"></a></span>
</pre></div>
</div>
<p>Now re-run the test to see the effect:</p>
<div class="code"><div class="highlight"><pre><span class="gp">$</span> bundle <span class="nb">exec </span>rspec spec/requests/static_pages_spec.rb
</pre></div>
</div>
<p>On my system, the passing test appears as in <a class="ref" href="static-pages.html#fig-green_passing_spec">Figure 3.7</a>.</p>
<div class="label" id="fig-green_passing_spec"></div>
<div class="figure"><div class="center"><span class="graphic"><img src="images/figures/green_passing_spec.png" alt="green_passing_spec" /></span></div><div class="caption"><span class="header">Figure 3.7: </span><span class="description">A green (passing) test. <a href="http://railstutorial.org/images/figures/green_passing_spec-full.png">(full size)</a></span></div></div>
<p>Based on the example for the Home page, you can probably guess the analogous test and application code for the Help page. We start by testing for the relevant content, in this case the string <code>’Help’</code> (<a class="ref" href="static-pages.html#code-help_content_spec">Listing 3.11</a>).</p>
<div class="label" id="code-help_content_spec"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 3.11.</span> <span class="description">Adding code to test the contents of the Help page. <br /> <code>spec/requests/static_pages_spec.rb</code></span> </div>
<div class="code"><div class="highlight"><pre><span class="nb">require</span> <span class="s1">'spec_helper'</span>
<span class="n">describe</span> <span class="s2">"Static pages"</span> <span class="k">do</span>
<span class="n">describe</span> <span class="s2">"Home page"</span> <span class="k">do</span>
<span class="n">it</span> <span class="s2">"should have the content 'Sample App'"</span> <span class="k">do</span>
<span class="n">visit</span> <span class="s1">'/static_pages/home'</span>
<span class="n">page</span><span class="o">.</span><span class="n">should</span> <span class="n">have_content</span><span class="p">(</span><span class="s1">'Sample App'</span><span class="p">)</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="n">describe</span> <span class="s2">"Help page"</span> <span class="k">do</span>
<span class="n">it</span> <span class="s2">"should have the content 'Help'"</span> <span class="k">do</span>
<span class="n">visit</span> <span class="s1">'/static_pages/help'</span>
<span class="n">page</span><span class="o">.</span><span class="n">should</span> <span class="n">have_content</span><span class="p">(</span><span class="s1">'Help'</span><span class="p">)</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">end</span>
</pre></div>
</div></div>
<p>Then run the tests:</p>
<div class="code"><div class="highlight"><pre><span class="gp">$</span> bundle <span class="nb">exec </span>rspec spec/requests/static_pages_spec.rb
</pre></div>
</div>
<p>One test should fail. (Since systems will vary, and since keeping track of how many tests there are at each stage of the tutorial is a maintenance nightmare, I’ll omit the RSpec output from now on.)</p>
<p>The application code (which for now is raw HTML) is similar to the code in <a class="ref" href="static-pages.html#code-home_page_passing">Listing 3.10</a>, as seen in <a class="ref" href="static-pages.html#code-help_page_passing">Listing 3.12</a>.</p>
<div class="label" id="code-help_page_passing"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 3.12.</span> <span class="description">Code to get a passing test for the Help page. <br /> <code>app/views/static_pages/help.html.erb</code></span> </div>
<div class="code"><div class="highlight"><pre><span class="nt"><h1></span>Help<span class="nt"></h1></span>
<span class="nt"><p></span>
Get help on the Ruby on Rails Tutorial at the
<span class="nt"><a</span> <span class="na">href=</span><span class="s">"http://railstutorial.org/help"</span><span class="nt">></span>Rails Tutorial help page<span class="nt"></a></span>.
To get help on this sample app, see the
<span class="nt"><a</span> <span class="na">href=</span><span class="s">"http://railstutorial.org/book"</span><span class="nt">></span>Rails Tutorial book<span class="nt"></a></span>.
<span class="nt"></p></span>
</pre></div>
</div></div>
<p>The tests should now pass:</p>
<div class="code"><div class="highlight"><pre><span class="gp">$</span> bundle <span class="nb">exec </span>rspec spec/requests/static_pages_spec.rb
</pre></div>
</div>
<div class="label" id="sec-adding_a_page"></div>
<h3><a id="sec-3_2_2" href="static-pages.html#sec-adding_a_page" class="heading"><span class="number">3.2.2</span> Adding a page</a></h3>
<p>Having seen test-driven development in action in a simple example, we’ll use the same technique to accomplish the slightly more complicated task of adding a new page, namely, the About page that we intentionally left off in <a class="ref" href="static-pages.html#sec-static_pages_with_rails">Section 3.1.2</a>. By writing a test and running RSpec at each step, we’ll see how TDD can guide us through the development of our application code.</p>
<div class="label" id="sec-red"></div>
<h4><a id="sec-3_2_2_1" href="static-pages.html#sec-red" class="heading">Red</a></h4>
<p>We’ll get to the Red part of the Red-Green cycle by writing a failing test for the About page. Following the models from <a class="ref" href="static-pages.html#code-help_content_spec">Listing 3.11</a>, you can probably guess the right test (<a class="ref" href="static-pages.html#code-about_page_content_spec">Listing 3.13</a>).</p>
<div class="label" id="code-about_page_content_spec"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 3.13.</span> <span class="description">Adding code to test the contents of the About page. <br /> <code>spec/requests/static_pages_spec.rb</code></span> </div>
<div class="code"><div class="highlight"><pre><span class="nb">require</span> <span class="s1">'spec_helper'</span>
<span class="n">describe</span> <span class="s2">"Static pages"</span> <span class="k">do</span>
<span class="n">describe</span> <span class="s2">"Home page"</span> <span class="k">do</span>
<span class="n">it</span> <span class="s2">"should have the content 'Sample App'"</span> <span class="k">do</span>
<span class="n">visit</span> <span class="s1">'/static_pages/home'</span>
<span class="n">page</span><span class="o">.</span><span class="n">should</span> <span class="n">have_content</span><span class="p">(</span><span class="s1">'Sample App'</span><span class="p">)</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="n">describe</span> <span class="s2">"Help page"</span> <span class="k">do</span>
<span class="n">it</span> <span class="s2">"should have the content 'Help'"</span> <span class="k">do</span>
<span class="n">visit</span> <span class="s1">'/static_pages/help'</span>
<span class="n">page</span><span class="o">.</span><span class="n">should</span> <span class="n">have_content</span><span class="p">(</span><span class="s1">'Help'</span><span class="p">)</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="n">describe</span> <span class="s2">"About page"</span> <span class="k">do</span>
<span class="n">it</span> <span class="s2">"should have the content 'About Us'"</span> <span class="k">do</span>
<span class="n">visit</span> <span class="s1">'/static_pages/about'</span>
<span class="n">page</span><span class="o">.</span><span class="n">should</span> <span class="n">have_content</span><span class="p">(</span><span class="s1">'About Us'</span><span class="p">)</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">end</span>
</pre></div>
</div></div>
<div class="label" id="sec-green"></div>
<h4><a id="sec-3_2_2_2" href="static-pages.html#sec-green" class="heading">Green</a></h4>
<p>Recall from <a class="ref" href="static-pages.html#sec-static_pages_with_rails">Section 3.1.2</a> that we can generate a static page in Rails by creating an action and corresponding view with the page’s name. In our case, the About page will first need an action called <code>about</code> in the StaticPages controller. Having written a failing test, we can now be confident that, in getting it to pass, we will actually have created a working About page.</p>
<p>If you run the RSpec example using</p>
<div class="code"><div class="highlight"><pre><span class="gp">$</span> bundle <span class="nb">exec </span>rspec spec/requests/static_pages_spec.rb
</pre></div>
</div>
<p>the output includes the following complaint:</p>
<div class="code"><div class="highlight"><pre>No route matches [GET] "/static_pages/about"
</pre></div>
</div>
<p>This is a hint that we need to add <code>/static_pages/about</code> to the routes file, which we can accomplish by following the pattern in <a class="ref" href="static-pages.html#code-pages_routes">Listing 3.5</a>, as shown in <a class="ref" href="static-pages.html#code-about_route">Listing 3.14</a>.</p>
<div class="label" id="code-about_route"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 3.14.</span> <span class="description">Adding the <code>about</code> route. <br /> <code>config/routes.rb</code></span> </div>
<div class="code"><div class="highlight"><pre><span class="ss">SampleApp::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">get</span> <span class="s2">"static_pages/home"</span>
<span class="n">get</span> <span class="s2">"static_pages/help"</span>
<span class="n">get</span> <span class="s2">"static_pages/about"</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="k">end</span>
</pre></div>
</div></div>
<p>Now running</p>
<div class="code"><div class="highlight"><pre><span class="gp">$</span> bundle <span class="nb">exec </span>rspec spec/requests/static_pages_spec.rb
</pre></div>
</div>
<p>complains that</p>
<div class="code"><div class="highlight"><pre>The action 'about' could not be found for StaticPagesController
</pre></div>
</div>
<p>To solve this problem, we follow the model provided by <code>home</code> and <code>help</code> from <a class="ref" href="static-pages.html#code-static_pages_controller">Listing 3.6</a> by adding an <code>about</code> action in the StaticPages controller (<a class="ref" href="static-pages.html#code-adding_the_about_page">Listing 3.15</a>).</p>
<div class="label" id="code-adding_the_about_page"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 3.15.</span> <span class="description">The StaticPages controller with added <code>about</code> action. <br /> <code>app/controllers/static_pages_controller.rb</code></span> </div>
<div class="code"><div class="highlight"><pre><span class="k">class</span> <span class="nc">StaticPagesController</span> <span class="o"><</span> <span class="no">ApplicationController</span>
<span class="k">def</span> <span class="nf">home</span>
<span class="k">end</span>
<span class="k">def</span> <span class="nf">help</span>
<span class="k">end</span>
<span class="k">def</span> <span class="nf">about</span>
<span class="k">end</span>
<span class="k">end</span>
</pre></div>
</div></div>
<p>Now running</p>
<div class="code"><div class="highlight"><pre><span class="gp">$</span> bundle <span class="nb">exec </span>rspec spec/requests/static_pages_spec.rb
</pre></div>
</div>
<p>says that we are missing a “template”, i.e., a view:</p>
<div class="code"><div class="highlight"><pre>ActionView::MissingTemplate:
Missing template static_pages/about
</pre></div>
</div>
<p>To solve this issue, we add the <code>about</code> view. This involves creating a new file called <code>about.html.erb</code> in the <code>app/views/static_pages</code> directory with the contents shown in <a class="ref" href="static-pages.html#code-about_view">Listing 3.16</a>.</p>
<div class="label" id="code-about_view"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 3.16.</span> <span class="description">Code for the About page. <br /> <code>app/views/static_pages/about.html.erb</code></span> </div>
<div class="code"><div class="highlight"><pre><span class="nt"><h1></span>About Us<span class="nt"></h1></span>
<span class="nt"><p></span>
The <span class="nt"><a</span> <span class="na">href=</span><span class="s">"http://railstutorial.org/"</span><span class="nt">></span>Ruby on Rails Tutorial<span class="nt"></a></span>
is a project to make a book and screencasts to teach web development
with <span class="nt"><a</span> <span class="na">href=</span><span class="s">"http://rubyonrails.org/"</span><span class="nt">></span>Ruby on Rails<span class="nt"></a></span>. This
is the sample application for the tutorial.
<span class="nt"></p></span>
</pre></div>
</div></div>
<p>Running RSpec should now get us back to Green:</p>
<div class="code"><div class="highlight"><pre><span class="gp">$</span> bundle <span class="nb">exec </span>rspec spec/requests/static_pages_spec.rb
</pre></div>
</div>
<p>Of course, it’s never a bad idea to take a look at the page in a browser to make sure our tests aren’t completely crazy (<a class="ref" href="static-pages.html#fig-about_us">Figure 3.8</a>).</p>
<div class="label" id="fig-about_us"></div>
<div class="figure"><div class="center"><span class="graphic"><img src="images/figures/about_us_2nd_edition.png" alt="about_us_2nd_edition" /></span></div><div class="caption"><span class="header">Figure 3.8: </span><span class="description">The new About page (<a href="http://localhost:3000/static_pages/about">/static_pages/about</a>). <a href="http://railstutorial.org/images/figures/about_us_2nd_edition-full.png">(full size)</a></span></div></div>
<div class="label" id="sec-refactor"></div>
<h4><a id="sec-3_2_2_3" href="static-pages.html#sec-refactor" class="heading">Refactor</a></h4>
<p>Now that we’ve gotten to Green, we are free to refactor our code with confidence. Oftentimes code will start to “smell”, meaning that it gets ugly, bloated, or filled with repetition. The computer doesn’t care, of course, but humans do, so it is important to keep the code base clean by refactoring frequently. Having a good test suite is an invaluable tool in this regard, as it dramatically lowers the probability of introducing bugs while refactoring.</p>
<p>Our sample app is a little too small to refactor right now, but code smell seeps in at every crack, so we won’t have to wait long: we’ll already get busy refactoring in <a class="ref" href="static-pages.html#sec-layouts">Section 3.3.4</a>.</p>
<div class="label" id="sec-slightly_dynamic_pages"></div>
<h2><a id="sec-3_3" href="static-pages.html#sec-slightly_dynamic_pages" class="heading"><span class="number">3.3</span> Slightly dynamic pages</a></h2>