# Routes ordering

**URL:** <https://www.ruby-forum.com/t/routes-ordering/193614>\
**Category:** Rails\
**Created:** [August 20, 2010, 2:23pm UTC](https://www.ruby-forum.com/t/routes-ordering/193614 "2010-08-20T14:23:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [August 20, 2010, 2:23pm UTC](https://www.ruby-forum.com/t/routes-ordering/193614/1 "2010-08-20T14:23:12Z")

</div>

In the “Head First Rails” book, it mentions this ordering in routes.rb:

ActionController: : Routing: : Routes. draw do | map|  
map. connect ’ /ads/new’ , : controller=\>’ ads’ , : action=\>’ new’  
map. connect ’ /ads/create’ , : controller=\>’ ads’ , : action=\>’  
create’  
map. connect ’ /ads/’ , : controller=\>’ ads’ , : action=\>’ index’  
map. connect ’ /ads/: id’ , : controller=\>’ ads’ , : action=\>’ show’

If we enter:

map. connect ’ /ads/: id’ , : controller=\>’ ads’ , : action=\>’ show’

At the top of the ordering, how will that affect our routing.

Can you just describe how to order routes in routes.rb? I mean, what is  
the rule to follow when ordering routes?

Thanks.

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [August 20, 2010, 2:50pm UTC](https://www.ruby-forum.com/t/routes-ordering/193614/2 "2010-08-20T14:50:19Z")

</div>

On 20 August 2010 13:23, Abder-Rahman A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> map. connect ’ /ads/: id’ , : controller=\>’ ads’ , : action=\>’ show’
> 
> At the top of the ordering, how will that affect our routing.
> 
> Can you just describe how to order routes in routes.rb? I mean, what is  
> the rule to follow when ordering routes?

The first entry in the file that matches is used, later ones are  
disregarded, so if you put the /ads/:id one at the start (note there  
is no space after the colon), then if a url matches that route then  
later ones will not be tested. You can always try it and see for  
yourself what happens. Have a look at the Rails Guide on routing for  
more information.

Colin

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [August 20, 2010, 3:09pm UTC](https://www.ruby-forum.com/t/routes-ordering/193614/3 "2010-08-20T15:09:12Z")

</div>

Abder-Rahman A. wrote:

> In the “Head First Rails” book, it mentions this ordering in routes.rb:
> 
> ActionController: : Routing: : Routes. draw do | map|  
> map. connect ’ /ads/new’ , : controller=\>’ ads’ , : action=\>’ new’  
> map. connect ’ /ads/create’ , : controller=\>’ ads’ , : action=\>’  
> create’  
> map. connect ’ /ads/’ , : controller=\>’ ads’ , : action=\>’ index’  
> map. connect ’ /ads/: id’ , : controller=\>’ ads’ , : action=\>’ show’
> 
> If we enter:
> 
> map. connect ’ /ads/: id’ , : controller=\>’ ads’ , : action=\>’ show’
> 
> At the top of the ordering, how will that affect our routing.
> 
> Can you just describe how to order routes in routes.rb? I mean, what is  
> the rule to follow when ordering routes?
> 
> Thanks.

I’m not sure how old “Head First Rails” is, though I’m thinking 2008. If  
you’re getting started take a look at Ruby on Rails Guides (as Colin  
mentioned), which has an excellent page for this topic:

> **[Rails Routing from the Outside In — Ruby on Rails Guides](https://guides.rubyonrails.org/routing.html)**
>
> 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...

Using RESTful routes simplifies that that example is trying to achieve  
by a lot, and using them I rarely run into the precedence issue:

map.resources :ads, :only =\> [:index, :new, :create, :show]

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [August 20, 2010, 5:12pm UTC](https://www.ruby-forum.com/t/routes-ordering/193614/4 "2010-08-20T17:12:45Z")

</div>

Thanks everyone. Yes, Parker, the “Head First Rails” is 2008.

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [August 20, 2010, 4:34pm UTC](https://www.ruby-forum.com/t/routes-ordering/193614/5 "2010-08-20T16:34:55Z")

</div>

Abder-Rahman A. wrote:

> In the “Head First Rails” book, it mentions this ordering in routes.rb:
> 
> ActionController: : Routing: : Routes. draw do | map|  
> map. connect ’ /ads/new’ , : controller=\>’ ads’ , : action=\>’ new’  
> map. connect ’ /ads/create’ , : controller=\>’ ads’ , : action=\>’  
> create’  
> map. connect ’ /ads/’ , : controller=\>’ ads’ , : action=\>’ index’  
> map. connect ’ /ads/: id’ , : controller=\>’ ads’ , : action=\>’ show’
> 
> If we enter:
> 
> map. connect ’ /ads/: id’ , : controller=\>’ ads’ , : action=\>’ show’
> 
> At the top of the ordering, how will that affect our routing.
> 
> Can you just describe how to order routes in routes.rb? I mean, what is  
> the rule to follow when ordering routes?

The first matched route rules. If you read any Rails routing reference,  
you’ll find that is says just this.

Once again, the bigger answer: spend less time posting and more time  
reading. Virtually every question you’ve asked on this list could have  
been answered by 30 seconds with Google and/or 5 minutes with  
documentation.

> Thanks.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
