-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouting.html
1779 lines (1639 loc) · 90.6 KB
/
routing.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 lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Rails Routing from the Outside In — Ruby on Rails Guides</title>
<link rel="stylesheet" type="text/css" href="stylesheets/style.css" data-turbolinks-track="reload">
<link rel="stylesheet" type="text/css" href="stylesheets/print.css" media="print">
<link rel="stylesheet" type="text/css" href="stylesheets/syntaxhighlighter/shCore.css" data-turbolinks-track="reload">
<link rel="stylesheet" type="text/css" href="stylesheets/syntaxhighlighter/shThemeRailsGuides.css" data-turbolinks-track="reload">
<link rel="stylesheet" type="text/css" href="stylesheets/fixes.css" data-turbolinks-track="reload">
<link href="images/favicon.ico" rel="shortcut icon" type="image/x-icon" />
<script src="javascripts/syntaxhighlighter.js" data-turbolinks-track="reload"></script>
<script src="javascripts/turbolinks.js" data-turbolinks-track="reload"></script>
<script src="javascripts/guides.js" data-turbolinks-track="reload"></script>
<script src="javascripts/responsive-tables.js" data-turbolinks-track="reload"></script>
<meta property="og:title" content="Rails Routing from the Outside In — Ruby on Rails Guides" />
<meta name="description" content="Rails Routing from the Outside InThis guide covers the user-facing features of Rails routing.After reading this guide, you will know: How to interpret the code in config/routes.rb. How to construct your own routes, using either the preferred resourceful style or the match method. How to declare route parameters, which are passed onto controller actions. How to automatically create paths and URLs using route helpers. Advanced techniques such as creating constraints and mounting Rack endpoints." />
<meta property="og:description" content="Rails Routing from the Outside InThis guide covers the user-facing features of Rails routing.After reading this guide, you will know: How to interpret the code in config/routes.rb. How to construct your own routes, using either the preferred resourceful style or the match method. How to declare route parameters, which are passed onto controller actions. How to automatically create paths and URLs using route helpers. Advanced techniques such as creating constraints and mounting Rack endpoints." />
<meta property="og:locale" content="en_US" />
<meta property="og:site_name" content="Ruby on Rails Guides" />
<meta property="og:image" content="https://avatars.githubusercontent.com/u/4223" />
<meta property="og:type" content="website" />
</head>
<body class="guide">
<div id="topNav">
<div class="wrapper">
<strong class="more-info-label">공식 웹사이트 <a href="https://rubyonrails.org/">rubyonrails.org:</a> </strong>
<span class="red-button more-info-button">
루비온레일스 웹사이트
</span>
<ul class="more-info-links s-hidden">
<li class="more-info"><a href="https://weblog.rubyonrails.org/">블로그</a></li>
<li class="more-info"><a href="https://guides.rubyonrails.org/">영문가이드</a></li>
<li class="more-info"><a href="https://api.rubyonrails.org/">레일스API</a></li>
<li class="more-info"><a href="https://stackoverflow.com/questions/tagged/ruby-on-rails">질문하기</a></li>
<li class="more-info"><a href="https://github.com/rails/rails">GitHub에서 기여하기</a></li>
</ul>
</div>
</div>
<div id="header">
<div class="wrapper clearfix">
<h1><a href="index.html" title="Return to home page">Guides.rubyonrails.org</a></h1>
<ul class="nav">
<li><a class="nav-item" href="index.html">홈</a></li>
<li class="guides-index guides-index-large">
<a href="index.html" id="guidesMenu" class="guides-index-item nav-item">가이드 인덱스</a>
<div id="guides" class="clearfix" style="display: none;">
<hr />
<div class="guides-section-container">
<div class="guides-section">
<dt>시작하면서</dt>
<dd><a href="getting_started.html">레일스로 시작하기</a></dd>
</div>
<div class="guides-section">
<dt>모델</dt>
<dd><a href="active_record_basics.html">액티브 레코드 기본</a></dd>
<dd><a href="active_record_migrations.html">액티브 레코드 마이그레이션</a></dd>
<dd><a href="active_record_validations.html">액티브 레코드 유효성 검증</a></dd>
<dd><a href="active_record_callbacks.html">액티브 레코드 콜백</a></dd>
<dd><a href="association_basics.html">Active Record Associations</a></dd>
<dd><a href="active_record_querying.html">Active Record Query Interface</a></dd>
</div>
<div class="guides-section">
<dt>Views</dt>
<dd><a href="layouts_and_rendering.html">Layouts and Rendering in Rails</a></dd>
<dd><a href="form_helpers.html">Action View Form Helpers</a></dd>
</div>
<div class="guides-section">
<dt>Controllers</dt>
<dd><a href="action_controller_overview.html">Action Controller Overview</a></dd>
<dd><a href="routing.html">Rails Routing from the Outside In</a></dd>
</div>
<div class="guides-section">
<dt>Other Components</dt>
<dd><a href="active_support_core_extensions.html">Active Support Core Extensions</a></dd>
<dd><a href="action_mailer_basics.html">Action Mailer Basics</a></dd>
<dd><a href="active_job_basics.html">Active Job Basics</a></dd>
<dd><a href="active_storage_overview.html">Active Storage Overview</a></dd>
<dd><a href="action_cable_overview.html">Action Cable Overview</a></dd>
</div>
<div class="guides-section">
<dt>Digging Deeper</dt>
<dd><a href="i18n.html">Rails Internationalization (I18n) API</a></dd>
<dd><a href="testing.html">Testing Rails Applications</a></dd>
<dd><a href="security.html">Securing Rails Applications</a></dd>
<dd><a href="debugging_rails_applications.html">Debugging Rails Applications</a></dd>
<dd><a href="configuring.html">Configuring Rails Applications</a></dd>
<dd><a href="command_line.html">The Rails Command Line</a></dd>
<dd><a href="asset_pipeline.html">The Asset Pipeline</a></dd>
<dd><a href="working_with_javascript_in_rails.html">Working with JavaScript in Rails</a></dd>
<dd><a href="autoloading_and_reloading_constants.html">Autoloading and Reloading Constants (Zeitwerk Mode)</a></dd>
<dd><a href="autoloading_and_reloading_constants_classic_mode.html">Autoloading and Reloading Constants (Classic Mode)</a></dd>
<dd><a href="caching_with_rails.html">Caching with Rails: An Overview</a></dd>
<dd><a href="api_app.html">Using Rails for API-only Applications</a></dd>
</div>
<div class="guides-section">
<dt>Extending Rails</dt>
<dd><a href="rails_on_rack.html">Rails on Rack</a></dd>
<dd><a href="generators.html">Creating and Customizing Rails Generators & Templates</a></dd>
</div>
<div class="guides-section">
<dt>Contributions</dt>
<dd><a href="contributing_to_ruby_on_rails.html">Contributing to Ruby on Rails</a></dd>
<dd><a href="api_documentation_guidelines.html">API Documentation Guidelines</a></dd>
<dd><a href="ruby_on_rails_guides_guidelines.html">Guides Guidelines</a></dd>
</div>
<div class="guides-section">
<dt>Policies</dt>
<dd><a href="maintenance_policy.html">Maintenance Policy</a></dd>
</div>
<div class="guides-section">
<dt>Release Notes</dt>
<dd><a href="upgrading_ruby_on_rails.html">Upgrading Ruby on Rails</a></dd>
<dd><a href="6_0_release_notes.html">Version 6.0 - August 2019</a></dd>
<dd><a href="5_2_release_notes.html">Version 5.2 - April 2018</a></dd>
<dd><a href="5_1_release_notes.html">Version 5.1 - April 2017</a></dd>
<dd><a href="5_0_release_notes.html">Version 5.0 - June 2016</a></dd>
<dd><a href="4_2_release_notes.html">Version 4.2 - December 2014</a></dd>
<dd><a href="4_1_release_notes.html">Version 4.1 - April 2014</a></dd>
<dd><a href="4_0_release_notes.html">Version 4.0 - June 2013</a></dd>
<dd><a href="3_2_release_notes.html">Version 3.2 - January 2012</a></dd>
<dd><a href="3_1_release_notes.html">Version 3.1 - August 2011</a></dd>
<dd><a href="3_0_release_notes.html">Version 3.0 - August 2010</a></dd>
<dd><a href="2_3_release_notes.html">Version 2.3 - March 2009</a></dd>
<dd><a href="2_2_release_notes.html">Version 2.2 - November 2008</a></dd>
</div>
</div>
</div>
</li>
<li><a class="nav-item" href="contributing_to_ruby_on_rails.html">기여하기</a></li>
<li class="guides-index guides-index-small">
<select class="guides-index-item nav-item">
<option value="index.html">가이드 인덱스</option>
<optgroup label="시작하면서">
<option value="getting_started.html">레일스로 시작하기</option>
</optgroup>
<optgroup label="모델">
<option value="active_record_basics.html">액티브 레코드 기본</option>
<option value="active_record_migrations.html">액티브 레코드 마이그레이션</option>
<option value="active_record_validations.html">액티브 레코드 유효성 검증</option>
<option value="active_record_callbacks.html">액티브 레코드 콜백</option>
<option value="association_basics.html">Active Record Associations</option>
<option value="active_record_querying.html">Active Record Query Interface</option>
</optgroup>
<optgroup label="Views">
<option value="layouts_and_rendering.html">Layouts and Rendering in Rails</option>
<option value="form_helpers.html">Action View Form Helpers</option>
</optgroup>
<optgroup label="Controllers">
<option value="action_controller_overview.html">Action Controller Overview</option>
<option value="routing.html">Rails Routing from the Outside In</option>
</optgroup>
<optgroup label="Other Components">
<option value="active_support_core_extensions.html">Active Support Core Extensions</option>
<option value="action_mailer_basics.html">Action Mailer Basics</option>
<option value="active_job_basics.html">Active Job Basics</option>
<option value="active_storage_overview.html">Active Storage Overview</option>
<option value="action_cable_overview.html">Action Cable Overview</option>
</optgroup>
<optgroup label="Digging Deeper">
<option value="i18n.html">Rails Internationalization (I18n) API</option>
<option value="testing.html">Testing Rails Applications</option>
<option value="security.html">Securing Rails Applications</option>
<option value="debugging_rails_applications.html">Debugging Rails Applications</option>
<option value="configuring.html">Configuring Rails Applications</option>
<option value="command_line.html">The Rails Command Line</option>
<option value="asset_pipeline.html">The Asset Pipeline</option>
<option value="working_with_javascript_in_rails.html">Working with JavaScript in Rails</option>
<option value="autoloading_and_reloading_constants.html">Autoloading and Reloading Constants (Zeitwerk Mode)</option>
<option value="autoloading_and_reloading_constants_classic_mode.html">Autoloading and Reloading Constants (Classic Mode)</option>
<option value="caching_with_rails.html">Caching with Rails: An Overview</option>
<option value="api_app.html">Using Rails for API-only Applications</option>
</optgroup>
<optgroup label="Extending Rails">
<option value="rails_on_rack.html">Rails on Rack</option>
<option value="generators.html">Creating and Customizing Rails Generators & Templates</option>
</optgroup>
<optgroup label="Contributions">
<option value="contributing_to_ruby_on_rails.html">Contributing to Ruby on Rails</option>
<option value="api_documentation_guidelines.html">API Documentation Guidelines</option>
<option value="ruby_on_rails_guides_guidelines.html">Guides Guidelines</option>
</optgroup>
<optgroup label="Policies">
<option value="maintenance_policy.html">Maintenance Policy</option>
</optgroup>
<optgroup label="Release Notes">
<option value="upgrading_ruby_on_rails.html">Upgrading Ruby on Rails</option>
<option value="6_0_release_notes.html">Version 6.0 - August 2019</option>
<option value="5_2_release_notes.html">Version 5.2 - April 2018</option>
<option value="5_1_release_notes.html">Version 5.1 - April 2017</option>
<option value="5_0_release_notes.html">Version 5.0 - June 2016</option>
<option value="4_2_release_notes.html">Version 4.2 - December 2014</option>
<option value="4_1_release_notes.html">Version 4.1 - April 2014</option>
<option value="4_0_release_notes.html">Version 4.0 - June 2013</option>
<option value="3_2_release_notes.html">Version 3.2 - January 2012</option>
<option value="3_1_release_notes.html">Version 3.1 - August 2011</option>
<option value="3_0_release_notes.html">Version 3.0 - August 2010</option>
<option value="2_3_release_notes.html">Version 2.3 - March 2009</option>
<option value="2_2_release_notes.html">Version 2.2 - November 2008</option>
</optgroup>
</select>
</li>
</ul>
</div>
</div>
<hr class="hide" />
<div id="feature">
<div class="wrapper">
<h2>Rails Routing from the Outside In</h2><p>This guide covers the user-facing features of Rails routing.</p><p>After reading this guide, you will know:</p>
<ul>
<li>How to interpret the code in <code>config/routes.rb</code>.</li>
<li>How to construct your own routes, using either the preferred resourceful style or the <code>match</code> method.</li>
<li>How to declare route parameters, which are passed onto controller actions.</li>
<li>How to automatically create paths and URLs using route helpers.</li>
<li>Advanced techniques such as creating constraints and mounting Rack endpoints.</li>
</ul>
<div id="subCol">
<h3 class="chapter"><img src="images/chapters_icon.gif" alt="" />Chapters</h3>
<ol class="chapters">
<li>
<a href="#the-purpose-of-the-rails-router">The Purpose of the Rails Router</a>
<ul>
<li><a href="#connecting-urls-to-code">Connecting URLs to Code</a></li>
<li><a href="#generating-paths-and-urls-from-code">Generating Paths and URLs from Code</a></li>
<li><a href="#configuring-the-rails-router">Configuring the Rails Router</a></li>
</ul>
</li>
<li>
<a href="#resource-routing-the-rails-default">Resource Routing: the Rails Default</a>
<ul>
<li><a href="#resources-on-the-web">Resources on the Web</a></li>
<li><a href="#crud-verbs-and-actions">CRUD, Verbs, and Actions</a></li>
<li><a href="#path-and-url-helpers">Path and URL Helpers</a></li>
<li><a href="#defining-multiple-resources-at-the-same-time">Defining Multiple Resources at the Same Time</a></li>
<li><a href="#singular-resources">Singular Resources</a></li>
<li><a href="#controller-namespaces-and-routing">Controller Namespaces and Routing</a></li>
<li><a href="#nested-resources">Nested Resources</a></li>
<li><a href="#routing-concerns">Routing concerns</a></li>
<li><a href="#creating-paths-and-urls-from-objects">Creating Paths and URLs From Objects</a></li>
<li><a href="#adding-more-restful-actions">Adding More RESTful Actions</a></li>
</ul>
</li>
<li>
<a href="#non-resourceful-routes">Non-Resourceful Routes</a>
<ul>
<li><a href="#bound-parameters">Bound Parameters</a></li>
<li><a href="#dynamic-segments">Dynamic Segments</a></li>
<li><a href="#static-segments">Static Segments</a></li>
<li><a href="#the-query-string">The Query String</a></li>
<li><a href="#defining-defaults">Defining Defaults</a></li>
<li><a href="#naming-routes">Naming Routes</a></li>
<li><a href="#http-verb-constraints">HTTP Verb Constraints</a></li>
<li><a href="#segment-constraints">Segment Constraints</a></li>
<li><a href="#request-based-constraints">Request-Based Constraints</a></li>
<li><a href="#advanced-constraints">Advanced Constraints</a></li>
<li><a href="#route-globbing-and-wildcard-segments">Route Globbing and Wildcard Segments</a></li>
<li><a href="#redirection">Redirection</a></li>
<li><a href="#routing-to-rack-applications">Routing to Rack Applications</a></li>
<li><a href="#using-root">Using <code>root</code></a></li>
<li><a href="#unicode-character-routes">Unicode character routes</a></li>
<li><a href="#direct-routes">Direct routes</a></li>
<li><a href="#using-resolve">Using <code>resolve</code></a></li>
</ul>
</li>
<li>
<a href="#customizing-resourceful-routes">Customizing Resourceful Routes</a>
<ul>
<li><a href="#specifying-a-controller-to-use">Specifying a Controller to Use</a></li>
<li><a href="#specifying-constraints">Specifying Constraints</a></li>
<li><a href="#overriding-the-named-route-helpers">Overriding the Named Route Helpers</a></li>
<li><a href="#overriding-the-new-and-edit-segments">Overriding the <code>new</code> and <code>edit</code> Segments</a></li>
<li><a href="#prefixing-the-named-route-helpers">Prefixing the Named Route Helpers</a></li>
<li><a href="#restricting-the-routes-created">Restricting the Routes Created</a></li>
<li><a href="#translated-paths">Translated Paths</a></li>
<li><a href="#overriding-the-singular-form">Overriding the Singular Form</a></li>
<li><a href="#using-as-in-nested-resources">Using <code>:as</code> in Nested Resources</a></li>
<li><a href="#overriding-named-route-parameters">Overriding Named Route Parameters</a></li>
</ul>
</li>
<li>
<a href="#inspecting-and-testing-routes">Inspecting and Testing Routes</a>
<ul>
<li><a href="#listing-existing-routes">Listing Existing Routes</a></li>
<li><a href="#testing-routes">Testing Routes</a></li>
</ul>
</li>
</ol>
</div>
</div>
</div>
<div id="container">
<div class="wrapper">
<div id="mainCol">
<h3 id="the-purpose-of-the-rails-router"><a class="anchorlink" href="#the-purpose-of-the-rails-router">1 The Purpose of the Rails Router</a></h3><p>The Rails router recognizes URLs and dispatches them to a controller's action, or to a Rack application. It can also generate paths and URLs, avoiding the need to hardcode strings in your views.</p><h4 id="connecting-urls-to-code"><a class="anchorlink" href="#connecting-urls-to-code">1.1 Connecting URLs to Code</a></h4><p>When your Rails application receives an incoming request for:</p><div class="code_container">
<pre class="brush: plain; gutter: false; toolbar: false">
GET /patients/17
</pre>
</div>
<p>it asks the router to match it to a controller action. If the first matching route is:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
get '/patients/:id', to: 'patients#show'
</pre>
</div>
<p>the request is dispatched to the <code>patients</code> controller's <code>show</code> action with <code>{ id: '17' }</code> in <code>params</code>.</p><div class="note"><p>Rails uses snake_case for controller names here, if you have a multiple word controller like <code>MonsterTrucksController</code>, you want to use <code>monster_trucks#show</code> for example.</p></div><h4 id="generating-paths-and-urls-from-code"><a class="anchorlink" href="#generating-paths-and-urls-from-code">1.2 Generating Paths and URLs from Code</a></h4><p>You can also generate paths and URLs. If the route above is modified to be:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
get '/patients/:id', to: 'patients#show', as: 'patient'
</pre>
</div>
<p>and your application contains this code in the controller:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
@patient = Patient.find(params[:id])
</pre>
</div>
<p>and this in the corresponding view:</p><div class="code_container">
<pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
<%= link_to 'Patient Record', patient_path(@patient) %>
</pre>
</div>
<p>then the router will generate the path <code>/patients/17</code>. This reduces the brittleness of your view and makes your code easier to understand. Note that the id does not need to be specified in the route helper.</p><h4 id="configuring-the-rails-router"><a class="anchorlink" href="#configuring-the-rails-router">1.3 Configuring the Rails Router</a></h4><p>The routes for your application or engine live in the file <code>config/routes.rb</code> and typically looks like this:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
Rails.application.routes.draw do
resources :brands, only: [:index, :show] do
resources :products, only: [:index, :show]
end
resource :basket, only: [:show, :update, :destroy]
resolve("Basket") { route_for(:basket) }
end
</pre>
</div>
<p>Since this is a regular Ruby source file you can use all of its features to help you define your routes but be careful with variable names as they can clash with the DSL methods of the router.</p><div class="note"><p>The <code>Rails.application.routes.draw do ... end</code> block that wraps your route definitions is required to establish the scope for the router DSL and must not be deleted.</p></div><h3 id="resource-routing-the-rails-default"><a class="anchorlink" href="#resource-routing-the-rails-default">2 Resource Routing: the Rails Default</a></h3><p>Resource routing allows you to quickly declare all of the common routes for a given resourceful controller. Instead of declaring separate routes for your <code>index</code>, <code>show</code>, <code>new</code>, <code>edit</code>, <code>create</code>, <code>update</code> and <code>destroy</code> actions, a resourceful route declares them in a single line of code.</p><h4 id="resources-on-the-web"><a class="anchorlink" href="#resources-on-the-web">2.1 Resources on the Web</a></h4><p>Browsers request pages from Rails by making a request for a URL using a specific HTTP method, such as <code>GET</code>, <code>POST</code>, <code>PATCH</code>, <code>PUT</code> and <code>DELETE</code>. Each method is a request to perform an operation on the resource. A resource route maps a number of related requests to actions in a single controller.</p><p>When your Rails application receives an incoming request for:</p><div class="code_container">
<pre class="brush: plain; gutter: false; toolbar: false">
DELETE /photos/17
</pre>
</div>
<p>it asks the router to map it to a controller action. If the first matching route is:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
resources :photos
</pre>
</div>
<p>Rails would dispatch that request to the <code>destroy</code> action on the <code>photos</code> controller with <code>{ id: '17' }</code> in <code>params</code>.</p><h4 id="crud-verbs-and-actions"><a class="anchorlink" href="#crud-verbs-and-actions">2.2 CRUD, Verbs, and Actions</a></h4><p>In Rails, a resourceful route provides a mapping between HTTP verbs and URLs to
controller actions. By convention, each action also maps to a specific CRUD
operation in a database. A single entry in the routing file, such as:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
resources :photos
</pre>
</div>
<p>creates seven different routes in your application, all mapping to the <code>Photos</code> controller:</p>
<table>
<thead>
<tr>
<th>HTTP Verb</th>
<th>Path</th>
<th>Controller#Action</th>
<th>Used for</th>
</tr>
</thead>
<tbody>
<tr>
<td>GET</td>
<td>/photos</td>
<td>photos#index</td>
<td>display a list of all photos</td>
</tr>
<tr>
<td>GET</td>
<td>/photos/new</td>
<td>photos#new</td>
<td>return an HTML form for creating a new photo</td>
</tr>
<tr>
<td>POST</td>
<td>/photos</td>
<td>photos#create</td>
<td>create a new photo</td>
</tr>
<tr>
<td>GET</td>
<td>/photos/:id</td>
<td>photos#show</td>
<td>display a specific photo</td>
</tr>
<tr>
<td>GET</td>
<td>/photos/:id/edit</td>
<td>photos#edit</td>
<td>return an HTML form for editing a photo</td>
</tr>
<tr>
<td>PATCH/PUT</td>
<td>/photos/:id</td>
<td>photos#update</td>
<td>update a specific photo</td>
</tr>
<tr>
<td>DELETE</td>
<td>/photos/:id</td>
<td>photos#destroy</td>
<td>delete a specific photo</td>
</tr>
</tbody>
</table>
<div class="note"><p>Because the router uses the HTTP verb and URL to match inbound requests, four URLs map to seven different actions.</p></div><div class="note"><p>Rails routes are matched in the order they are specified, so if you have a <code>resources :photos</code> above a <code>get 'photos/poll'</code> the <code>show</code> action's route for the <code>resources</code> line will be matched before the <code>get</code> line. To fix this, move the <code>get</code> line <strong>above</strong> the <code>resources</code> line so that it is matched first.</p></div><h4 id="path-and-url-helpers"><a class="anchorlink" href="#path-and-url-helpers">2.3 Path and URL Helpers</a></h4><p>Creating a resourceful route will also expose a number of helpers to the controllers in your application. In the case of <code>resources :photos</code>:</p>
<ul>
<li>
<code>photos_path</code> returns <code>/photos</code>
</li>
<li>
<code>new_photo_path</code> returns <code>/photos/new</code>
</li>
<li>
<code>edit_photo_path(:id)</code> returns <code>/photos/:id/edit</code> (for instance, <code>edit_photo_path(10)</code> returns <code>/photos/10/edit</code>)</li>
<li>
<code>photo_path(:id)</code> returns <code>/photos/:id</code> (for instance, <code>photo_path(10)</code> returns <code>/photos/10</code>)</li>
</ul>
<p>Each of these helpers has a corresponding <code>_url</code> helper (such as <code>photos_url</code>) which returns the same path prefixed with the current host, port, and path prefix.</p><h4 id="defining-multiple-resources-at-the-same-time"><a class="anchorlink" href="#defining-multiple-resources-at-the-same-time">2.4 Defining Multiple Resources at the Same Time</a></h4><p>If you need to create routes for more than one resource, you can save a bit of typing by defining them all with a single call to <code>resources</code>:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
resources :photos, :books, :videos
</pre>
</div>
<p>This works exactly the same as:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
resources :photos
resources :books
resources :videos
</pre>
</div>
<h4 id="singular-resources"><a class="anchorlink" href="#singular-resources">2.5 Singular Resources</a></h4><p>Sometimes, you have a resource that clients always look up without referencing an ID. For example, you would like <code>/profile</code> to always show the profile of the currently logged in user. In this case, you can use a singular resource to map <code>/profile</code> (rather than <code>/profile/:id</code>) to the <code>show</code> action:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
get 'profile', to: 'users#show'
</pre>
</div>
<p>Passing a <code>String</code> to <code>to:</code> will expect a <code>controller#action</code> format. When using a <code>Symbol</code>, the <code>to:</code> option should be replaced with <code>action:</code>. When using a <code>String</code> without a <code>#</code>, the <code>to:</code> option should be replaced with <code>controller:</code>:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
get 'profile', action: :show, controller: 'users'
</pre>
</div>
<p>This resourceful route:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
resource :geocoder
resolve('Geocoder') { [:geocoder] }
</pre>
</div>
<p>creates six different routes in your application, all mapping to the <code>Geocoders</code> controller:</p>
<table>
<thead>
<tr>
<th>HTTP Verb</th>
<th>Path</th>
<th>Controller#Action</th>
<th>Used for</th>
</tr>
</thead>
<tbody>
<tr>
<td>GET</td>
<td>/geocoder/new</td>
<td>geocoders#new</td>
<td>return an HTML form for creating the geocoder</td>
</tr>
<tr>
<td>POST</td>
<td>/geocoder</td>
<td>geocoders#create</td>
<td>create the new geocoder</td>
</tr>
<tr>
<td>GET</td>
<td>/geocoder</td>
<td>geocoders#show</td>
<td>display the one and only geocoder resource</td>
</tr>
<tr>
<td>GET</td>
<td>/geocoder/edit</td>
<td>geocoders#edit</td>
<td>return an HTML form for editing the geocoder</td>
</tr>
<tr>
<td>PATCH/PUT</td>
<td>/geocoder</td>
<td>geocoders#update</td>
<td>update the one and only geocoder resource</td>
</tr>
<tr>
<td>DELETE</td>
<td>/geocoder</td>
<td>geocoders#destroy</td>
<td>delete the geocoder resource</td>
</tr>
</tbody>
</table>
<div class="note"><p>Because you might want to use the same controller for a singular route (<code>/account</code>) and a plural route (<code>/accounts/45</code>), singular resources map to plural controllers. So that, for example, <code>resource :photo</code> and <code>resources :photos</code> creates both singular and plural routes that map to the same controller (<code>PhotosController</code>).</p></div><p>A singular resourceful route generates these helpers:</p>
<ul>
<li>
<code>new_geocoder_path</code> returns <code>/geocoder/new</code>
</li>
<li>
<code>edit_geocoder_path</code> returns <code>/geocoder/edit</code>
</li>
<li>
<code>geocoder_path</code> returns <code>/geocoder</code>
</li>
</ul>
<p>As with plural resources, the same helpers ending in <code>_url</code> will also include the host, port, and path prefix.</p><h4 id="controller-namespaces-and-routing"><a class="anchorlink" href="#controller-namespaces-and-routing">2.6 Controller Namespaces and Routing</a></h4><p>You may wish to organize groups of controllers under a namespace. Most commonly, you might group a number of administrative controllers under an <code>Admin::</code> namespace. You would place these controllers under the <code>app/controllers/admin</code> directory, and you can group them together in your router:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
namespace :admin do
resources :articles, :comments
end
</pre>
</div>
<p>This will create a number of routes for each of the <code>articles</code> and <code>comments</code> controller. For <code>Admin::ArticlesController</code>, Rails will create:</p>
<table>
<thead>
<tr>
<th>HTTP Verb</th>
<th>Path</th>
<th>Controller#Action</th>
<th>Named Route Helper</th>
</tr>
</thead>
<tbody>
<tr>
<td>GET</td>
<td>/admin/articles</td>
<td>admin/articles#index</td>
<td>admin_articles_path</td>
</tr>
<tr>
<td>GET</td>
<td>/admin/articles/new</td>
<td>admin/articles#new</td>
<td>new_admin_article_path</td>
</tr>
<tr>
<td>POST</td>
<td>/admin/articles</td>
<td>admin/articles#create</td>
<td>admin_articles_path</td>
</tr>
<tr>
<td>GET</td>
<td>/admin/articles/:id</td>
<td>admin/articles#show</td>
<td>admin_article_path(:id)</td>
</tr>
<tr>
<td>GET</td>
<td>/admin/articles/:id/edit</td>
<td>admin/articles#edit</td>
<td>edit_admin_article_path(:id)</td>
</tr>
<tr>
<td>PATCH/PUT</td>
<td>/admin/articles/:id</td>
<td>admin/articles#update</td>
<td>admin_article_path(:id)</td>
</tr>
<tr>
<td>DELETE</td>
<td>/admin/articles/:id</td>
<td>admin/articles#destroy</td>
<td>admin_article_path(:id)</td>
</tr>
</tbody>
</table>
<p>If you want to route <code>/articles</code> (without the prefix <code>/admin</code>) to <code>Admin::ArticlesController</code>, you could use:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
scope module: 'admin' do
resources :articles, :comments
end
</pre>
</div>
<p>or, for a single case:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
resources :articles, module: 'admin'
</pre>
</div>
<p>If you want to route <code>/admin/articles</code> to <code>ArticlesController</code> (without the <code>Admin::</code> module prefix), you could use:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
scope '/admin' do
resources :articles, :comments
end
</pre>
</div>
<p>or, for a single case:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
resources :articles, path: '/admin/articles'
</pre>
</div>
<p>In each of these cases, the named routes remain the same as if you did not use <code>scope</code>. In the last case, the following paths map to <code>ArticlesController</code>:</p>
<table>
<thead>
<tr>
<th>HTTP Verb</th>
<th>Path</th>
<th>Controller#Action</th>
<th>Named Route Helper</th>
</tr>
</thead>
<tbody>
<tr>
<td>GET</td>
<td>/admin/articles</td>
<td>articles#index</td>
<td>articles_path</td>
</tr>
<tr>
<td>GET</td>
<td>/admin/articles/new</td>
<td>articles#new</td>
<td>new_article_path</td>
</tr>
<tr>
<td>POST</td>
<td>/admin/articles</td>
<td>articles#create</td>
<td>articles_path</td>
</tr>
<tr>
<td>GET</td>
<td>/admin/articles/:id</td>
<td>articles#show</td>
<td>article_path(:id)</td>
</tr>
<tr>
<td>GET</td>
<td>/admin/articles/:id/edit</td>
<td>articles#edit</td>
<td>edit_article_path(:id)</td>
</tr>
<tr>
<td>PATCH/PUT</td>
<td>/admin/articles/:id</td>
<td>articles#update</td>
<td>article_path(:id)</td>
</tr>
<tr>
<td>DELETE</td>
<td>/admin/articles/:id</td>
<td>articles#destroy</td>
<td>article_path(:id)</td>
</tr>
</tbody>
</table>
<div class="info"><p><em>If you need to use a different controller namespace inside a <code>namespace</code> block you can specify an absolute controller path, e.g: <code>get '/foo', to: '/foo#index'</code>.</em></p></div><h4 id="nested-resources"><a class="anchorlink" href="#nested-resources">2.7 Nested Resources</a></h4><p>It's common to have resources that are logically children of other resources. For example, suppose your application includes these models:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
class Magazine < ApplicationRecord
has_many :ads
end
class Ad < ApplicationRecord
belongs_to :magazine
end
</pre>
</div>
<p>Nested routes allow you to capture this relationship in your routing. In this case, you could include this route declaration:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
resources :magazines do
resources :ads
end
</pre>
</div>
<p>In addition to the routes for magazines, this declaration will also route ads to an <code>AdsController</code>. The ad URLs require a magazine:</p>
<table>
<thead>
<tr>
<th>HTTP Verb</th>
<th>Path</th>
<th>Controller#Action</th>
<th>Used for</th>
</tr>
</thead>
<tbody>
<tr>
<td>GET</td>
<td>/magazines/:magazine_id/ads</td>
<td>ads#index</td>
<td>display a list of all ads for a specific magazine</td>
</tr>
<tr>
<td>GET</td>
<td>/magazines/:magazine_id/ads/new</td>
<td>ads#new</td>
<td>return an HTML form for creating a new ad belonging to a specific magazine</td>
</tr>
<tr>
<td>POST</td>
<td>/magazines/:magazine_id/ads</td>
<td>ads#create</td>
<td>create a new ad belonging to a specific magazine</td>
</tr>
<tr>
<td>GET</td>
<td>/magazines/:magazine_id/ads/:id</td>
<td>ads#show</td>
<td>display a specific ad belonging to a specific magazine</td>
</tr>
<tr>
<td>GET</td>
<td>/magazines/:magazine_id/ads/:id/edit</td>
<td>ads#edit</td>
<td>return an HTML form for editing an ad belonging to a specific magazine</td>
</tr>
<tr>
<td>PATCH/PUT</td>
<td>/magazines/:magazine_id/ads/:id</td>
<td>ads#update</td>
<td>update a specific ad belonging to a specific magazine</td>
</tr>
<tr>
<td>DELETE</td>
<td>/magazines/:magazine_id/ads/:id</td>
<td>ads#destroy</td>
<td>delete a specific ad belonging to a specific magazine</td>
</tr>
</tbody>
</table>
<p>This will also create routing helpers such as <code>magazine_ads_url</code> and <code>edit_magazine_ad_path</code>. These helpers take an instance of Magazine as the first parameter (<code>magazine_ads_url(@magazine)</code>).</p><h5 id="limits-to-nesting"><a class="anchorlink" href="#limits-to-nesting">2.7.1 Limits to Nesting</a></h5><p>You can nest resources within other nested resources if you like. For example:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
resources :publishers do
resources :magazines do
resources :photos
end
end
</pre>
</div>
<p>Deeply-nested resources quickly become cumbersome. In this case, for example, the application would recognize paths such as:</p><div class="code_container">
<pre class="brush: plain; gutter: false; toolbar: false">
/publishers/1/magazines/2/photos/3
</pre>
</div>
<p>The corresponding route helper would be <code>publisher_magazine_photo_url</code>, requiring you to specify objects at all three levels. Indeed, this situation is confusing enough that a popular <a href="http://weblog.jamisbuck.org/2007/2/5/nesting-resources">article</a> by Jamis Buck proposes a rule of thumb for good Rails design:</p><div class="info"><p><em>Resources should never be nested more than 1 level deep.</em></p></div><h5 id="shallow-nesting"><a class="anchorlink" href="#shallow-nesting">2.7.2 Shallow Nesting</a></h5><p>One way to avoid deep nesting (as recommended above) is to generate the collection actions scoped under the parent, so as to get a sense of the hierarchy, but to not nest the member actions. In other words, to only build routes with the minimal amount of information to uniquely identify the resource, like this:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
resources :articles do
resources :comments, only: [:index, :new, :create]
end
resources :comments, only: [:show, :edit, :update, :destroy]
</pre>
</div>
<p>This idea strikes a balance between descriptive routes and deep nesting. There exists shorthand syntax to achieve just that, via the <code>:shallow</code> option:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
resources :articles do
resources :comments, shallow: true
end
</pre>
</div>
<p>This will generate the exact same routes as the first example. You can also specify the <code>:shallow</code> option in the parent resource, in which case all of the nested resources will be shallow:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
resources :articles, shallow: true do
resources :comments
resources :quotes
resources :drafts
end
</pre>
</div>
<p>The <code>shallow</code> method of the DSL creates a scope inside of which every nesting is shallow. This generates the same routes as the previous example:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
shallow do
resources :articles do
resources :comments
resources :quotes
resources :drafts
end
end
</pre>
</div>
<p>There exist two options for <code>scope</code> to customize shallow routes. <code>:shallow_path</code> prefixes member paths with the specified parameter:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
scope shallow_path: "sekret" do
resources :articles do
resources :comments, shallow: true
end
end
</pre>
</div>
<p>The comments resource here will have the following routes generated for it:</p>
<table>
<thead>
<tr>
<th>HTTP Verb</th>
<th>Path</th>
<th>Controller#Action</th>
<th>Named Route Helper</th>
</tr>
</thead>
<tbody>
<tr>
<td>GET</td>
<td>/articles/:article_id/comments(.:format)</td>
<td>comments#index</td>
<td>article_comments_path</td>
</tr>
<tr>
<td>POST</td>
<td>/articles/:article_id/comments(.:format)</td>
<td>comments#create</td>
<td>article_comments_path</td>
</tr>
<tr>
<td>GET</td>
<td>/articles/:article_id/comments/new(.:format)</td>
<td>comments#new</td>
<td>new_article_comment_path</td>
</tr>
<tr>
<td>GET</td>
<td>/sekret/comments/:id/edit(.:format)</td>
<td>comments#edit</td>
<td>edit_comment_path</td>
</tr>
<tr>
<td>GET</td>
<td>/sekret/comments/:id(.:format)</td>
<td>comments#show</td>
<td>comment_path</td>
</tr>
<tr>
<td>PATCH/PUT</td>
<td>/sekret/comments/:id(.:format)</td>
<td>comments#update</td>
<td>comment_path</td>
</tr>
<tr>
<td>DELETE</td>
<td>/sekret/comments/:id(.:format)</td>
<td>comments#destroy</td>
<td>comment_path</td>
</tr>
</tbody>
</table>
<p>The <code>:shallow_prefix</code> option adds the specified parameter to the named route helpers:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
scope shallow_prefix: "sekret" do
resources :articles do
resources :comments, shallow: true
end
end
</pre>
</div>
<p>The comments resource here will have the following routes generated for it:</p>
<table>
<thead>
<tr>
<th>HTTP Verb</th>
<th>Path</th>
<th>Controller#Action</th>
<th>Named Route Helper</th>
</tr>
</thead>
<tbody>
<tr>
<td>GET</td>
<td>/articles/:article_id/comments(.:format)</td>
<td>comments#index</td>
<td>article_comments_path</td>
</tr>
<tr>
<td>POST</td>
<td>/articles/:article_id/comments(.:format)</td>
<td>comments#create</td>
<td>article_comments_path</td>
</tr>
<tr>
<td>GET</td>
<td>/articles/:article_id/comments/new(.:format)</td>
<td>comments#new</td>
<td>new_article_comment_path</td>
</tr>
<tr>
<td>GET</td>
<td>/comments/:id/edit(.:format)</td>
<td>comments#edit</td>
<td>edit_sekret_comment_path</td>
</tr>
<tr>
<td>GET</td>
<td>/comments/:id(.:format)</td>
<td>comments#show</td>
<td>sekret_comment_path</td>
</tr>
<tr>
<td>PATCH/PUT</td>
<td>/comments/:id(.:format)</td>
<td>comments#update</td>
<td>sekret_comment_path</td>
</tr>
<tr>
<td>DELETE</td>
<td>/comments/:id(.:format)</td>
<td>comments#destroy</td>
<td>sekret_comment_path</td>
</tr>
</tbody>
</table>
<h4 id="routing-concerns"><a class="anchorlink" href="#routing-concerns">2.8 Routing concerns</a></h4><p>Routing concerns allow you to declare common routes that can be reused inside other resources and routes. To define a concern:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
concern :commentable do
resources :comments
end
concern :image_attachable do
resources :images, only: :index
end
</pre>
</div>
<p>These concerns can be used in resources to avoid code duplication and share behavior across routes:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
resources :messages, concerns: :commentable
resources :articles, concerns: [:commentable, :image_attachable]
</pre>
</div>
<p>The above is equivalent to:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
resources :messages do
resources :comments
end
resources :articles do
resources :comments
resources :images, only: :index
end
</pre>
</div>
<p>Also you can use them in any place that you want inside the routes, for example in a <code>scope</code> or <code>namespace</code> call:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
namespace :articles do
concerns :commentable
end
</pre>
</div>
<h4 id="creating-paths-and-urls-from-objects"><a class="anchorlink" href="#creating-paths-and-urls-from-objects">2.9 Creating Paths and URLs From Objects</a></h4><p>In addition to using the routing helpers, Rails can also create paths and URLs from an array of parameters. For example, suppose you have this set of routes:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
resources :magazines do
resources :ads
end
</pre>
</div>
<p>When using <code>magazine_ad_path</code>, you can pass in instances of <code>Magazine</code> and <code>Ad</code> instead of the numeric IDs:</p><div class="code_container">
<pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
<%= link_to 'Ad details', magazine_ad_path(@magazine, @ad) %>
</pre>
</div>
<p>You can also use <code>url_for</code> with a set of objects, and Rails will automatically determine which route you want:</p><div class="code_container">
<pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
<%= link_to 'Ad details', url_for([@magazine, @ad]) %>
</pre>
</div>
<p>In this case, Rails will see that <code>@magazine</code> is a <code>Magazine</code> and <code>@ad</code> is an <code>Ad</code> and will therefore use the <code>magazine_ad_path</code> helper. In helpers like <code>link_to</code>, you can specify just the object in place of the full <code>url_for</code> call:</p><div class="code_container">
<pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">