# Actioncontroller- How the routes be choosen

**URL:** <https://www.ruby-forum.com/t/actioncontroller-how-the-routes-be-choosen/141179>\
**Category:** Deployment\
**Created:** [June 23, 2008, 10:32am UTC](https://www.ruby-forum.com/t/actioncontroller-how-the-routes-be-choosen/141179 "2008-06-23T10:32:36Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![zhonghaijun](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/838e76/32.png) [@zhonghaijun](https://www.ruby-forum.com/u/zhonghaijun)\
**Post date:** [June 23, 2008, 10:32am UTC](https://www.ruby-forum.com/t/actioncontroller-how-the-routes-be-choosen/141179/1 "2008-06-23T10:32:36Z")

</div>

Hi,

I am looking at the source code of resource.rb and have a question for  
the code below:

===========Ruby script resource.rb =======================  
map.named\_route("#{action}_#{resource.name\_prefix}#{resource.singular}",  
“#{resource.member\_path}#{resource.action\_separator}#{action}”,  
action\_options)  
map.connect("#{resource.member\_path};#{action}", action\_options)  
map.connect("#{resource.member\_path}.:format;#{action}", action\_options)  
map.named\_route("formatted_#{action}\_#{resource.name\_prefix}#{resource.singular}",  
“#{resource.member\_path}#{resource.action\_separator}#{action}.:format”,  
action\_options)  
============Ruby script resource.rb=======================

In my understanding, there would be four routes generated by method  
named\_route and connect.

So, here comes the question: How can rails identify the correct route in  
the four? and why put four routes in the hash table?

I roughly read other part of the code:  
==================Ruby script routing.rb==========================  
routes = routes\_by\_controller[controller][action][options.keys.sort\_by {  
|x| x.object\_id }]

routes.each do |route|  
results = route.send(method, options, merged, expire\_on)  
return results if results && (!results.is\_a?(Array) ||  
results.first)  
end  
===================Ruby script routing.rb===========================

It seems that the “routes” can be retrieved from Hash table and the  
first route in the hash table would be the route be selected.

Thanks for your time and any comments!

HaiJun
