This repository has been archived by the owner on Jan 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
routing.html
1460 lines (1367 loc) · 64.2 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 PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Ruby on Rails Guides: Rails Routing from the Outside In</title>
<link rel="stylesheet" type="text/css" href="stylesheets/style.css" />
<link rel="stylesheet" type="text/css" href="stylesheets/print.css" media="print" />
<link rel="stylesheet" type="text/css" href="stylesheets/syntaxhighlighter/shCore.css" />
<link rel="stylesheet" type="text/css" href="stylesheets/syntaxhighlighter/shThemeRailsGuides.css" />
</head>
<body class="guide">
<div id="topNav">
<div class="wrapper">
<strong>More at <a href="http://rubyonrails.org/">rubyonrails.org:</a> </strong>
<a href="http://rubyonrails.org/">Overview</a> |
<a href="http://rubyonrails.org/download">Download</a> |
<a href="http://rubyonrails.org/deploy">Deploy</a> |
<a href="http://rails.lighthouseapp.com/projects/8994-ruby-on-rails/overview">Code</a> |
<a href="http://rubyonrails.org/screencasts">Screencasts</a> |
<a href="http://rubyonrails.org/documentation">Documentation</a> |
<a href="http://rubyonrails.org/ecosystem">Ecosystem</a> |
<a href="http://rubyonrails.org/community">Community</a> |
<a href="http://weblog.rubyonrails.org/">Blog</a>
</div>
</div>
<div id="header">
<div class="wrapper clearfix">
<h1><a href="index.html" title="Return to home page">Guides.rubyonrails.org</a></h1>
<p class="hide"><a href="#mainCol">Skip navigation</a>.</p>
<ul class="nav">
<li><a href="index.html">홈</a></li>
<li class="index"><a href="index.html" onclick="guideMenu(); return false;" id="guidesMenu">목차</a>
<div id="guides" class="clearfix" style="display: none;">
<hr />
<dl class="L">
<dt>시작</dt>
<dd><a href="getting_started.html">레일즈 시작하기</a></dd>
<dt>모델(Models)</dt>
<dd><a href="migrations.html">레일즈 데이터베이스 마이그레이션</a></dd>
<dd><a href="active_record_validations_callbacks.html">액티브 레코드 데이터 검증(Validation)과 Callback(콜백)</a></dd>
<dd><a href="association_basics.html">액티브 레코드 Association(관계)</a></dd>
<dd><a href="active_record_querying.html">액티브 레코드 쿼리 인터페이스</a></dd>
<dt>뷰(Views)</dt>
<dd><a href="layouts_and_rendering.html">레이아웃(Layouts)과 렌더링(Rendering)</a></dd>
<dd><a href="form_helpers.html">액션 뷰 폼 핼퍼(Action View Form Helpers)</a></dd>
<dt>컨트롤러(Controllers)</dt>
<dd><a href="action_controller_overview.html">액션 컨트롤러 둘러보기</a></dd>
<dd><a href="routing.html">외부 요청에 대한 레일즈 라우팅</a></dd>
</dl>
<dl class="R">
<dt>심화내용</dt>
<dd><a href="active_support_core_extensions.html">액티브 서포트(Active Support) 확장(Core Extensions)</a></dd>
<dd><a href="i18n.html">레일즈 국제화I(nternationalization) API</a></dd>
<dd><a href="action_mailer_basics.html">액션 메일러의 기본</a></dd>
<dd><a href="testing.html">레일즈 어플리케이션 테스트하기</a></dd>
<dd><a href="security.html">레일즈 어플리케이션의 보안</a></dd>
<dd><a href="debugging_rails_applications.html">레일즈 어플리케이션 디버깅</a></dd>
<dd><a href="performance_testing.html">레일즈 어플리케이션 성능 테스트하기</a></dd>
<dd><a href="configuring.html">레일즈 어플리케이션 설정</a></dd>
<dd><a href="command_line.html">레일즈 커멘드라인 도구와 Rake 테스크</a></dd>
<dd><a href="caching_with_rails.html">레일즈를 이용한 캐싱</a></dd>
<dt>레일즈 확장하기(Extending Rails)</dt>
<dd><a href="plugins.html">레일즈 플러그인 작성의 기본</a></dd>
<dd><a href="rails_on_rack.html">렉 위의 레일즈(Rails on Rack)</a></dd>
<dd><a href="generators.html">레일즈 제너레이터(Generator) 제작과 수정</a></dd>
<dt>루비 온 레이즈에 기여하기</dt>
<dd><a href="contributing_to_ruby_on_rails.html">루비 온 레이즈에 기여하기</a></dd>
<dd><a href="api_documentation_guidelines.html">API 문서 가이드라인</a></dd>
<dd><a href="ruby_on_rails_guides_guidelines.html">루비 온 레일즈 가이드에 대한 가이드라인</a></dd>
<dt>Release Notes</dt>
<dd><a href="3_0_release_notes.html">Ruby on Rails 3.0 Release Notes</a></dd>
<dd><a href="2_3_release_notes.html">Ruby on Rails 2.3 Release Notes</a></dd>
<dd><a href="2_2_release_notes.html">Ruby on Rails 2.2 Release Notes</a></dd>
</dl>
</div>
</li>
<li><a href="contribute.html">기여하기</a></li>
<li><a href="credits.html">수고하신 분들</a></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. By referring to this guide, you will be able to:</p>
<ul>
<li>Understand the code in <tt>routes.rb</tt></li>
<li>Construct your own routes, using either the preferred resourceful style or the <tt>match</tt> method</li>
<li>Identify what parameters to expect an action to receive</li>
<li>Automatically create paths and URLs using route helpers</li>
<li>Use advanced techniques such as constraints and 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></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"><span class="caps">CRUD</span>, Verbs, and Actions</a></li> <li><a href="#paths-and-urls">Paths and URLs</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="#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"><span class="caps">HTTP</span> 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">Route Globbing</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 <tt>root</tt></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-helpers">Overriding the Named Helpers</a></li> <li><a href="#overriding-the-new-and-edit-segments">Overriding the <tt>new</tt> and <tt>edit</tt> 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="#nested-names">Using <tt>:as</tt> in Nested Resources</a></li></ul></li><li><a href="#inspecting-and-testing-routes">Inspecting and Testing Routes</a><ul><li><a href="#seeing-existing-routes-with-rake">Seeing Existing Routes with <tt>rake</tt></a></li> <li><a href="#testing-routes">Testing Routes</a></li></ul></li><li><a href="#changelog">Changelog</a></li></ol></div>
</div>
</div>
<div id="container">
<div class="wrapper">
<div id="mainCol">
<h3 id="the-purpose-of-the-rails-router">1 The Purpose of the Rails Router</h3>
<p>The Rails router recognizes URLs and dispatches them to a controller’s action. It can also generate paths and URLs, avoiding the need to hardcode strings in your views.</p>
<h4 id="connecting-urls-to-code">1.1 Connecting URLs to Code</h4>
<p>When your Rails application receives an incoming request</p>
<notextile>
<div class="code_container">
<pre class="brush: plain; gutter: false; toolbar: false">
GET /patients/17
</pre>
</div>
</notextile>
<p>it asks the router to match it to a controller action. If the first matching route is</p>
<notextile>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
match "/patients/:id" => "patients#show"
</pre>
</div>
</notextile>
<p>the request is dispatched to the <tt>patients</tt> controller’s <tt>show</tt> action with <tt>{ :id => “17” }</tt> in <tt>params</tt>.</p>
<h4 id="generating-paths-and-urls-from-code">1.2 Generating Paths and URLs from Code</h4>
<p>You can also generate paths and URLs. If your application contains this code:</p>
<notextile>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
@patient = Patient.find(17)
</pre>
</div>
</notextile>
<notextile>
<div class="code_container">
<pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
<%= link_to "Patient Record", patient_path(@patient) %>
</pre>
</div>
</notextile>
<p>The router will generate the path <tt>/patients/17</tt>. 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>
<h3 id="resource-routing-the-rails-default">2 Resource Routing: the Rails Default</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 <tt>index</tt>, <tt>show</tt>, <tt>new</tt>, <tt>edit</tt>, <tt>create</tt>, <tt>update</tt> and <tt>destroy</tt> actions, a resourceful route declares them in a single line of code.</p>
<h4 id="resources-on-the-web">2.1 Resources on the Web</h4>
<p>Browsers request pages from Rails by making a request for a <span class="caps">URL</span> using a specific <span class="caps">HTTP</span> method, such as <tt>GET</tt>, <tt>POST</tt>, <tt>PUT</tt> and <tt>DELETE</tt>. 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>
<notextile>
<div class="code_container">
<pre class="brush: plain; gutter: false; toolbar: false">
DELETE /photos/17
</pre>
</div>
</notextile>
<p>it asks the router to map it to a controller action. If the first matching route is</p>
<notextile>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
resources :photos
</pre>
</div>
</notextile>
<p>Rails would dispatch that request to the <tt>destroy</tt> method on the <tt>photos</tt> controller with <tt>{ :id => “17” }</tt> in <tt>params</tt>.</p>
<h4 id="crud-verbs-and-actions">2.2 <span class="caps">CRUD</span>, Verbs, and Actions</h4>
<p>In Rails, a resourceful route provides a mapping between <span class="caps">HTTP</span> verbs and URLs and controller actions. By convention, each action also maps to particular <span class="caps">CRUD</span> operations in a database. A single entry in the routing file, such as</p>
<notextile>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
resources :photos
</pre>
</div>
</notextile>
<p>creates seven different routes in your application, all mapping to the <tt>Photos</tt> controller:</p>
<table>
<tr>
<th><span class="caps">HTTP</span> Verb </th>
<th>Path </th>
<th>action </th>
<th>used for </th>
</tr>
<tr>
<td><span class="caps">GET</span> </td>
<td>/photos </td>
<td>index </td>
<td>display a list of all photos </td>
</tr>
<tr>
<td><span class="caps">GET</span> </td>
<td>/photos/new </td>
<td>new </td>
<td>return an <span class="caps">HTML</span> form for creating a new photo </td>
</tr>
<tr>
<td><span class="caps">POST</span> </td>
<td>/photos </td>
<td>create </td>
<td>create a new photo </td>
</tr>
<tr>
<td><span class="caps">GET</span> </td>
<td>/photos/:id </td>
<td>show </td>
<td>display a specific photo </td>
</tr>
<tr>
<td><span class="caps">GET</span> </td>
<td>/photos/:id/edit </td>
<td>edit </td>
<td>return an <span class="caps">HTML</span> form for editing a photo </td>
</tr>
<tr>
<td><span class="caps">PUT</span> </td>
<td>/photos/:id </td>
<td>update </td>
<td>update a specific photo </td>
</tr>
<tr>
<td><span class="caps">DELETE</span> </td>
<td>/photos/:id </td>
<td>destroy </td>
<td>delete a specific photo </td>
</tr>
</table>
<div class='note'><p>Rails routes are matched in the order they are specified, so if you have a <tt>resources :photos</tt> above a <tt>get 'photos/poll'</tt> the <tt>show</tt> action’s route for the <tt>resources</tt> line will be matched before the <tt>get</tt> line. To fix this, move the <tt>get</tt> line <strong>above</strong> the <tt>resources</tt> line so that it is matched first.</p></div>
<h4 id="paths-and-urls">2.3 Paths and URLs</h4>
<p>Creating a resourceful route will also expose a number of helpers to the controllers in your application. In the case of <tt>resources :photos</tt>:</p>
<ul>
<li><tt>photos_path</tt> returns <tt>/photos</tt></li>
<li><tt>new_photo_path</tt> returns <tt>/photos/new</tt></li>
<li><tt>edit_photo_path(id)</tt> returns <tt>/photos/:id/edit</tt> (for instance, <tt>edit_photo_path(10)</tt> returns <tt>/photos/10/edit</tt>)</li>
<li><tt>photo_path(id)</tt> returns <tt>/photos/:id</tt> (for instance, <tt>photo_path(10)</tt> returns <tt>/photos/10</tt>)</li>
</ul>
<p>Each of these helpers has a corresponding <tt>_url</tt> helper (such as <tt>photos_url</tt>) which returns the same path prefixed with the current host, port and path prefix.</p>
<div class='note'><p>Because the router uses the <span class="caps">HTTP</span> verb and <span class="caps">URL</span> to match inbound requests, four URLs map to seven different actions.</p></div>
<h4 id="defining-multiple-resources-at-the-same-time">2.4 Defining Multiple Resources at the Same Time</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 <tt>resources</tt>:</p>
<notextile>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
resources :photos, :books, :videos
</pre>
</div>
</notextile>
<p>This works exactly the same as</p>
<notextile>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
resources :photos
resources :books
resources :videos
</pre>
</div>
</notextile>
<h4 id="singular-resources">2.5 Singular Resources</h4>
<p>Sometimes, you have a resource that clients always look up without referencing an ID. For example, you would like <tt>/profile</tt> to always show the profile of the currently logged in user. In this case, you can use a singular resource to map <tt>/profile</tt> (rather than <tt>/profile/:id</tt>) to the <tt>show</tt> action.</p>
<notextile>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
match "profile" => "users#show"
</pre>
</div>
</notextile>
<p>This resourceful route</p>
<notextile>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
resource :geocoder
</pre>
</div>
</notextile>
<p>creates six different routes in your application, all mapping to the <tt>Geocoders</tt> controller:</p>
<table>
<tr>
<th><span class="caps">HTTP</span> Verb </th>
<th>Path </th>
<th>action </th>
<th>used for </th>
</tr>
<tr>
<td><span class="caps">GET</span> </td>
<td>/geocoder/new </td>
<td>new </td>
<td>return an <span class="caps">HTML</span> form for creating the geocoder </td>
</tr>
<tr>
<td><span class="caps">POST</span> </td>
<td>/geocoder </td>
<td>create </td>
<td>create the new geocoder </td>
</tr>
<tr>
<td><span class="caps">GET</span> </td>
<td>/geocoder </td>
<td>show </td>
<td>display the one and only geocoder resource </td>
</tr>
<tr>
<td><span class="caps">GET</span> </td>
<td>/geocoder/edit </td>
<td>edit </td>
<td>return an <span class="caps">HTML</span> form for editing the geocoder </td>
</tr>
<tr>
<td><span class="caps">PUT</span> </td>
<td>/geocoder </td>
<td>update </td>
<td>update the one and only geocoder resource </td>
</tr>
<tr>
<td><span class="caps">DELETE</span> </td>
<td>/geocoder </td>
<td>destroy </td>
<td>delete the geocoder resource </td>
</tr>
</table>
<div class='note'><p>Because you might want to use the same controller for a singular route (<tt>/account</tt>) and a plural route (<tt>/accounts/45</tt>), singular resources map to plural controllers.</p></div>
<p>A singular resourceful route generates these helpers:</p>
<ul>
<li><tt>new_geocoder_path</tt> returns <tt>/geocoder/new</tt></li>
<li><tt>edit_geocoder_path</tt> returns <tt>/geocoder/edit</tt></li>
<li><tt>geocoder_path</tt> returns <tt>/geocoder</tt></li>
</ul>
<p>As with plural resources, the same helpers ending in <tt>_url</tt> will also include the host, port and path prefix.</p>
<h4 id="controller-namespaces-and-routing">2.6 Controller Namespaces and Routing</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 <tt>Admin::</tt> namespace. You would place these controllers under the <tt>app/controllers/admin</tt> directory, and you can group them together in your router:</p>
<notextile>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
namespace :admin do
resources :posts, :comments
end
</pre>
</div>
</notextile>
<p>This will create a number of routes for each of the <tt>posts</tt> and <tt>comments</tt> controller. For <tt>Admin::PostsController</tt>, Rails will create:</p>
<table>
<tr>
<th><span class="caps">HTTP</span> Verb </th>
<th>Path </th>
<th>action </th>
<th>named helper </th>
</tr>
<tr>
<td><span class="caps">GET</span> </td>
<td>/admin/posts </td>
<td>index </td>
<td> admin_posts_path </td>
</tr>
<tr>
<td><span class="caps">GET</span> </td>
<td>/admin/posts/new </td>
<td>new </td>
<td> new_admin_posts_path </td>
</tr>
<tr>
<td><span class="caps">POST</span> </td>
<td>/admin/posts </td>
<td>create </td>
<td> admin_posts_path </td>
</tr>
<tr>
<td><span class="caps">GET</span> </td>
<td>/admin/posts/1 </td>
<td>show </td>
<td> admin_post_path(id) </td>
</tr>
<tr>
<td><span class="caps">GET</span> </td>
<td>/admin/posts/1/edit </td>
<td>edit </td>
<td> edit_admin_post_path(id) </td>
</tr>
<tr>
<td><span class="caps">PUT</span> </td>
<td>/admin/posts/1 </td>
<td>update </td>
<td> admin_post_path(id) </td>
</tr>
<tr>
<td><span class="caps">DELETE</span> </td>
<td>/admin/posts/1 </td>
<td>destroy </td>
<td> admin_post_path(id) </td>
</tr>
</table>
<p>If you want to route <tt>/posts</tt> (without the prefix <tt>/admin</tt>) to <tt>Admin::PostsController</tt>, you could use</p>
<notextile>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
scope :module => "admin" do
resources :posts, :comments
end
</pre>
</div>
</notextile>
<p>or, for a single case</p>
<notextile>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
resources :posts, :module => "admin"
</pre>
</div>
</notextile>
<p>If you want to route <tt>/admin/posts</tt> to <tt>PostsController</tt> (without the <tt>Admin::</tt> module prefix), you could use</p>
<notextile>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
scope "/admin" do
resources :posts, :comments
end
</pre>
</div>
</notextile>
<p>or, for a single case</p>
<notextile>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
resources :posts, :path => "/admin/posts"
</pre>
</div>
</notextile>
<p>In each of these cases, the named routes remain the same as if you did not use <tt>scope</tt>. In the last case, the following paths map to <tt>PostsController</tt>:</p>
<table>
<tr>
<th><span class="caps">HTTP</span> Verb </th>
<th>Path </th>
<th>action </th>
<th>named helper </th>
</tr>
<tr>
<td><span class="caps">GET</span> </td>
<td>/admin/posts </td>
<td>index </td>
<td> posts_path </td>
</tr>
<tr>
<td><span class="caps">GET</span> </td>
<td>/admin/posts/new </td>
<td>new </td>
<td> posts_path </td>
</tr>
<tr>
<td><span class="caps">POST</span> </td>
<td>/admin/posts </td>
<td>create </td>
<td> posts_path </td>
</tr>
<tr>
<td><span class="caps">GET</span> </td>
<td>/admin/posts/1 </td>
<td>show </td>
<td> post_path(id) </td>
</tr>
<tr>
<td><span class="caps">GET</span> </td>
<td>/admin/posts/1/edit </td>
<td>edit </td>
<td> edit_post_path(id) </td>
</tr>
<tr>
<td><span class="caps">PUT</span> </td>
<td>/admin/posts/1 </td>
<td>update </td>
<td> post_path(id) </td>
</tr>
<tr>
<td><span class="caps">DELETE</span> </td>
<td>/admin/posts/1 </td>
<td>destroy </td>
<td> post_path(id) </td>
</tr>
</table>
<h4 id="nested-resources">2.7 Nested Resources</h4>
<p>It’s common to have resources that are logically children of other resources. For example, suppose your application includes these models:</p>
<notextile>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
class Magazine < ActiveRecord::Base
has_many :ads
end
class Ad < ActiveRecord::Base
belongs_to :magazine
end
</pre>
</div>
</notextile>
<p>Nested routes allow you to capture this relationship in your routing. In this case, you could include this route declaration:</p>
<notextile>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
resources :magazines do
resources :ads
end
</pre>
</div>
</notextile>
<p>In addition to the routes for magazines, this declaration will also route ads to an <tt>AdsController</tt>. The ad URLs require a magazine:</p>
<table>
<tr>
<th><span class="caps">HTTP</span> Verb </th>
<th>Path </th>
<th>action </th>
<th>used for </th>
</tr>
<tr>
<td><span class="caps">GET</span> </td>
<td>/magazines/1/ads </td>
<td>index </td>
<td>display a list of all ads for a specific magazine </td>
</tr>
<tr>
<td><span class="caps">GET</span> </td>
<td>/magazines/1/ads/new </td>
<td>new </td>
<td>return an <span class="caps">HTML</span> form for creating a new ad belonging to a specific magazine </td>
</tr>
<tr>
<td><span class="caps">POST</span> </td>
<td>/magazines/1/ads </td>
<td>create </td>
<td>create a new ad belonging to a specific magazine </td>
</tr>
<tr>
<td><span class="caps">GET</span> </td>
<td>/magazines/1/ads/1 </td>
<td>show </td>
<td>display a specific ad belonging to a specific magazine </td>
</tr>
<tr>
<td><span class="caps">GET</span> </td>
<td>/magazines/1/ads/1/edit </td>
<td>edit </td>
<td>return an <span class="caps">HTML</span> form for editing an ad belonging to a specific magazine </td>
</tr>
<tr>
<td><span class="caps">PUT</span> </td>
<td>/magazines/1/ads/1 </td>
<td>update </td>
<td>update a specific ad belonging to a specific magazine </td>
</tr>
<tr>
<td><span class="caps">DELETE</span> </td>
<td>/magazines/1/ads/1 </td>
<td>destroy </td>
<td>delete a specific ad belonging to a specific magazine </td>
</tr>
</table>
<p>This will also create routing helpers such as <tt>magazine_ads_url</tt> and <tt>edit_magazine_ad_path</tt>. These helpers take an instance of Magazine as the first parameter (<tt>magazine_ads_url(@magazine)</tt>).</p>
<h5 id="limits-to-nesting">2.7.1 Limits to Nesting</h5>
<p>You can nest resources within other nested resources if you like. For example:</p>
<notextile>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
resources :publishers do
resources :magazines do
resources :photos
end
end
</pre>
</div>
</notextile>
<p>Deeply-nested resources quickly become cumbersome. In this case, for example, the application would recognize paths such as</p>
<pre>
/publishers/1/magazines/2/photos/3
</pre>
<p>The corresponding route helper would be <tt>publisher_magazine_photo_url</tt>, 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>
<h4 id="creating-paths-and-urls-from-objects">2.8 Creating Paths and URLs From Objects</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>
<notextile>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
resources :magazines do
resources :ads
end
</pre>
</div>
</notextile>
<p>When using <tt>magazine_ad_path</tt>, you can pass in instances of <tt>Magazine</tt> and <tt>Ad</tt> instead of the numeric IDs.</p>
<notextile>
<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>
</notextile>
<p>You can also use <tt>url_for</tt> with a set of objects, and Rails will automatically determine which route you want:</p>
<notextile>
<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>
</notextile>
<p>In this case, Rails will see that <tt>@magazine</tt> is a <tt>Magazine</tt> and <tt>@ad</tt> is an <tt>Ad</tt> and will therefore use the <tt>magazine_ad_path</tt> helper. In helpers like <tt>link_to</tt>, you can specify just the object in place of the full <tt>url_for</tt> call:</p>
<notextile>
<div class="code_container">
<pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
<%= link_to "Ad details", [@magazine, @ad] %>
</pre>
</div>
</notextile>
<p>If you wanted to link to just a magazine, you could leave out the <tt>Array</tt>:</p>
<notextile>
<div class="code_container">
<pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
<%= link_to "Magazine details", @magazine %>
</pre>
</div>
</notextile>
<p>This allows you to treat instances of your models as URLs, and is a key advantage to using the resourceful style.</p>
<h4 id="adding-more-restful-actions">2.9 Adding More RESTful Actions</h4>
<p>You are not limited to the seven routes that RESTful routing creates by default. If you like, you may add additional routes that apply to the collection or individual members of the collection.</p>
<h5 id="adding-member-routes">2.9.1 Adding Member Routes</h5>
<p>To add a member route, just add a <tt>member</tt> block into the resource block:</p>
<notextile>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
resources :photos do
member do
get 'preview'
end
end
</pre>
</div>
</notextile>
<p>This will recognize <tt>/photos/1/preview</tt> with <span class="caps">GET</span>, and route to the <tt>preview</tt> action of <tt>PhotosController</tt>. It will also create the <tt>preview_photo_url</tt> and <tt>preview_photo_path</tt> helpers.</p>
<p>Within the block of member routes, each route name specifies the <span class="caps">HTTP</span> verb that it will recognize. You can use <tt>get</tt>, <tt>put</tt>, <tt>post</tt>, or <tt>delete</tt> here. If you don’t have multiple <tt>member</tt> routes, you can also pass <tt>:on</tt> to a route, eliminating the block:</p>
<notextile>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
resources :photos do
get 'preview', :on => :member
end
</pre>
</div>
</notextile>
<h5 id="adding-collection-routes">2.9.2 Adding Collection Routes</h5>
<p>To add a route to the collection:</p>
<notextile>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
resources :photos do
collection do
get 'search'
end
end
</pre>
</div>
</notextile>
<p>This will enable Rails to recognize paths such as <tt>/photos/search</tt> with <span class="caps">GET</span>, and route to the <tt>search</tt> action of <tt>PhotosController</tt>. It will also create the <tt>search_photos_url</tt> and <tt>search_photos_path</tt> route helpers.</p>
<p>Just as with member routes, you can pass <tt>:on</tt> to a route:</p>
<notextile>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
resources :photos do
get 'search', :on => :collection
end
</pre>
</div>
</notextile>
<h5 id="a-note-of-caution">2.9.3 A Note of Caution</h5>
<p>If you find yourself adding many extra actions to a resourceful route, it’s time to stop and ask yourself whether you’re disguising the presence of another resource.</p>
<h3 id="non-resourceful-routes">3 Non-Resourceful Routes</h3>
<p>In addition to resource routing, Rails has powerful support for routing arbitrary URLs to actions. Here, you don’t get groups of routes automatically generated by resourceful routing. Instead, you set up each route within your application separately.</p>
<p>While you should usually use resourceful routing, there are still many places where the simpler routing is more appropriate. There’s no need to try to shoehorn every last piece of your application into a resourceful framework if that’s not a good fit.</p>
<p>In particular, simple routing makes it very easy to map legacy URLs to new Rails actions.</p>
<h4 id="bound-parameters">3.1 Bound Parameters</h4>
<p>When you set up a regular route, you supply a series of symbols that Rails maps to parts of an incoming <span class="caps">HTTP</span> request. Two of these symbols are special: <tt>:controller</tt> maps to the name of a controller in your application, and <tt>:action</tt> maps to the name of an action within that controller. For example, consider one of the default Rails routes:</p>
<notextile>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
match ':controller(/:action(/:id))'
</pre>
</div>
</notextile>
<p>If an incoming request of <tt>/photos/show/1</tt> is processed by this route (because it hasn’t matched any previous route in the file), then the result will be to invoke the <tt>show</tt> action of the <tt>PhotosController</tt>, and to make the final parameter <tt>"1"</tt> available as <tt>params[:id]</tt>. This route will also route the incoming request of <tt>/photos</tt> to <tt>PhotosController#index</tt>, since <tt>:action</tt> and <tt>:id</tt> are optional parameters, denoted by parentheses.</p>
<h4 id="dynamic-segments">3.2 Dynamic Segments</h4>
<p>You can set up as many dynamic segments within a regular route as you like. Anything other than <tt>:controller</tt> or <tt>:action</tt> will be available to the action as part of <tt>params</tt>. If you set up this route:</p>
<notextile>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
match ':controller/:action/:id/:user_id'
</pre>
</div>
</notextile>
<p>An incoming path of <tt>/photos/show/1/2</tt> will be dispatched to the <tt>show</tt> action of the <tt>PhotosController</tt>. <tt>params[:id]</tt> will be <tt>"1"</tt>, and <tt>params[:user_id]</tt> will be <tt>"2"</tt>.</p>
<div class='note'><p>You can’t use <tt>namespace</tt> or <tt>:module</tt> with a <tt>:controller</tt> path segment. If you need to do this then use a constraint on :controller that matches the namespace you require. e.g:</p></div>
<notextile>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
match ':controller(/:action(/:id))', :controller => /admin\/[^\/]+/
</pre>
</div>
</notextile>
<h4 id="static-segments">3.3 Static Segments</h4>
<p>You can specify static segments when creating a route:</p>
<notextile>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
match ':controller/:action/:id/with_user/:user_id'
</pre>
</div>
</notextile>
<p>This route would respond to paths such as <tt>/photos/show/1/with_user/2</tt>. In this case, <tt>params</tt> would be <tt>{ :controller => “photos”, :action => “show”, :id => “1”, :user_id => “2” }</tt>.</p>
<h4 id="the-query-string">3.4 The Query String</h4>
<p>The <tt>params</tt> will also include any parameters from the query string. For example, with this route:</p>
<notextile>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
match ':controller/:action/:id'
</pre>
</div>
</notextile>
<p>An incoming path of <tt>/photos/show/1?user_id=2</tt> will be dispatched to the <tt>show</tt> action of the <tt>Photos</tt> controller. <tt>params</tt> will be <tt>{ :controller => “photos”, :action => “show”, :id => “1”, :user_id => “2” }</tt>.</p>
<h4 id="defining-defaults">3.5 Defining Defaults</h4>
<p>You do not need to explicitly use the <tt>:controller</tt> and <tt>:action</tt> symbols within a route. You can supply them as defaults:</p>
<notextile>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
match 'photos/:id' => 'photos#show'
</pre>
</div>
</notextile>
<p>With this route, Rails will match an incoming path of <tt>/photos/12</tt> to the <tt>show</tt> action of <tt>PhotosController</tt>.</p>
<p>You can also define other defaults in a route by supplying a hash for the <tt>:defaults</tt> option. This even applies to parameters that you do not specify as dynamic segments. For example:</p>
<notextile>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
match 'photos/:id' => 'photos#show', :defaults => { :format => 'jpg' }
</pre>
</div>
</notextile>
<p>Rails would match <tt>photos/12</tt> to the <tt>show</tt> action of <tt>PhotosController</tt>, and set <tt>params[:format]</tt> to <tt>"jpg"</tt>.</p>
<h4 id="naming-routes">3.6 Naming Routes</h4>
<p>You can specify a name for any route using the <tt>:as</tt> option.</p>
<notextile>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
match 'exit' => 'sessions#destroy', :as => :logout
</pre>
</div>
</notextile>
<p>This will create <tt>logout_path</tt> and <tt>logout_url</tt> as named helpers in your application. Calling <tt>logout_path</tt> will return <tt>/exit</tt></p>
<h4 id="http-verb-constraints">3.7 <span class="caps">HTTP</span> Verb Constraints</h4>
<p>You can use the <tt>:via</tt> option to constrain the request to one or more <span class="caps">HTTP</span> methods:</p>
<notextile>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
match 'photos/show' => 'photos#show', :via => :get
</pre>
</div>
</notextile>
<p>There is a shorthand version of this as well:</p>
<notextile>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
get 'photos/show'
</pre>
</div>
</notextile>
<p>You can also permit more than one verb to a single route:</p>
<notextile>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
match 'photos/show' => 'photos#show', :via => [:get, :post]
</pre>
</div>
</notextile>
<h4 id="segment-constraints">3.8 Segment Constraints</h4>
<p>You can use the <tt>:constraints</tt> option to enforce a format for a dynamic segment:</p>
<notextile>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
match 'photos/:id' => 'photos#show', :constraints => { :id => /[A-Z]\d{5}/ }
</pre>
</div>
</notextile>
<p>This route would match paths such as <tt>/photos/A12345</tt>. You can more succinctly express the same route this way:</p>
<notextile>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
match 'photos/:id' => 'photos#show', :id => /[A-Z]\d{5}/
</pre>
</div>
</notextile>
<p><tt>:constraints</tt> takes regular expressions with the restriction that regexp anchors can’t be used. For example, the following route will not work:</p>
<notextile>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
match '/:id' => 'posts#show', :constraints => {:id => /^\d/}
</pre>
</div>
</notextile>
<p>However, note that you don’t need to use anchors because all routes are anchored at the start.</p>
<p>For example, the following routes would allow for <tt>posts</tt> with <tt>to_param</tt> values like <tt>1-hello-world</tt> that always begin with a number and <tt>users</tt> with <tt>to_param</tt> values like <tt>david</tt> that never begin with a number to share the root namespace:</p>
<notextile>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
match '/:id' => 'posts#show', :constraints => { :id => /\d.+/ }
match '/:username' => 'users#show'
</pre>
</div>
</notextile>
<h4 id="request-based-constraints">3.9 Request-Based Constraints</h4>
<p>You can also constrain a route based on any method on the <a href="action_controller_overview.html#the-request-object">Request</a> object that returns a <tt>String</tt>.</p>
<p>You specify a request-based constraint the same way that you specify a segment constraint:</p>
<notextile>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
match "photos", :constraints => {:subdomain => "admin"}
</pre>
</div>
</notextile>
<p>You can also specify constraints in a block form:</p>
<notextile>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
namespace :admin do
constraints :subdomain => "admin" do
resources :photos
end
end
</pre>
</div>
</notextile>
<h4 id="advanced-constraints">3.10 Advanced Constraints</h4>
<p>If you have a more advanced constraint, you can provide an object that responds to <tt>matches?</tt> that Rails should use. Let’s say you wanted to route all users on a blacklist to the <tt>BlacklistController</tt>. You could do:</p>
<notextile>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
class BlacklistConstraint
def initialize
@ips = Blacklist.retrieve_ips
end
def matches?(request)
@ips.include?(request.remote_ip)
end
end
TwitterClone::Application.routes.draw do
match "*path" => "blacklist#index",
:constraints => BlacklistConstraint.new
end
</pre>
</div>
</notextile>
<h4 id="route-globbing">3.11 Route Globbing</h4>
<p>Route globbing is a way to specify that a particular parameter should be matched to all the remaining parts of a route. For example</p>
<notextile>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
match 'photos/*other' => 'photos#unknown'
</pre>
</div>
</notextile>
<p>This route would match <tt>photos/12</tt> or <tt>/photos/long/path/to/12</tt>, setting <tt>params[:other]</tt> to <tt>"12"</tt> or <tt>"long/path/to/12"</tt>.</p>
<p>Wildcard segments can occur anywhere in a route. For example,</p>
<notextile>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
match 'books/*section/:title' => 'books#show'
</pre>
</div>
</notextile>
<p>would match <tt>books/some/section/last-words-a-memoir</tt> with <tt>params[:section]</tt> equals <tt>"some/section"</tt>, and <tt>params[:title]</tt> equals <tt>"last-words-a-memoir"</tt>.</p>
<p>Technically a route can have even more than one wildcard segment. The matcher assigns segments to parameters in an intuitive way. For example,</p>
<notextile>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
match '*a/foo/*b' => 'test#index'
</pre>
</div>
</notextile>
<p>would match <tt>zoo/woo/foo/bar/baz</tt> with <tt>params[:a]</tt> equals <tt>"zoo/woo"</tt>, and <tt>params[:b]</tt> equals <tt>"bar/baz"</tt>.</p>
<h4 id="redirection">3.12 Redirection</h4>
<p>You can redirect any path to another path using the <tt>redirect</tt> helper in your router:</p>
<notextile>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
match "/stories" => redirect("/posts")
</pre>
</div>
</notextile>
<p>You can also reuse dynamic segments from the match in the path to redirect to:</p>
<notextile>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
match "/stories/:name" => redirect("/posts/%{name}")
</pre>
</div>
</notextile>
<p>You can also provide a block to redirect, which receives the params and (optionally) the request object:</p>
<notextile>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
match "/stories/:name" => redirect {|params| "/posts/#{params[:name].pluralize}" }
match "/stories" => redirect {|p, req| "/posts/#{req.subdomain}" }
</pre>
</div>
</notextile>
<p>In all of these cases, if you don’t provide the leading host (<tt>http://www.example.com</tt>), Rails will take those details from the current request.</p>
<h4 id="routing-to-rack-applications">3.13 Routing to Rack Applications</h4>
<p>Instead of a String, like <tt>"posts#index"</tt>, which corresponds to the <tt>index</tt> action in the <tt>PostsController</tt>, you can specify any <a href="rails_on_rack.html">Rack application</a> as the endpoint for a matcher.</p>
<notextile>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
match "/application.js" => Sprockets
</pre>
</div>
</notextile>
<p>As long as <tt>Sprockets</tt> responds to <tt>call</tt> and returns a <tt>[status, headers, body]</tt>, the router won’t know the difference between the Rack application and an action.</p>
<div class='note'><p>For the curious, <tt>"posts#index"</tt> actually expands out to <tt>PostsController.action(:index)</tt>, which returns a valid Rack application.</p></div>
<h4 id="using-root">3.14 Using <tt>root</tt></h4>
<p>You can specify what Rails should route <tt>"/"</tt> to with the <tt>root</tt> method:</p>
<notextile>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
root :to => 'pages#main'
</pre>
</div>
</notextile>
<p>You should put the <tt>root</tt> route at the end of the file. You also need to delete the <tt>public/index.html</tt> file for the root route to take effect.</p>
<h3 id="customizing-resourceful-routes">4 Customizing Resourceful Routes</h3>
<p>While the default routes and helpers generated by <tt>resources :posts</tt> will usually serve you well, you may want to customize them in some way. Rails allows you to customize virtually any generic part of the resourceful helpers.</p>
<h4 id="specifying-a-controller-to-use">4.1 Specifying a Controller to Use</h4>
<p>The <tt>:controller</tt> option lets you explicitly specify a controller to use for the resource. For example:</p>
<notextile>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
resources :photos, :controller => "images"
</pre>
</div>
</notextile>
<p>will recognize incoming paths beginning with <tt>/photos</tt> but route to the <tt>Images</tt> controller:</p>