# Sophisticated routes recognition (with regexp, for instance). Is there any way?

**URL:** <https://www.ruby-forum.com/t/sophisticated-routes-recognition-with-regexp-for-instance-is-there-any-way/177278>\
**Category:** Rails\
**Created:** [October 14, 2009, 1:52pm UTC](https://www.ruby-forum.com/t/sophisticated-routes-recognition-with-regexp-for-instance-is-there-any-way/177278 "2009-10-14T13:52:25Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![daniel304](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e5b9ba/32.png) [@daniel304](https://www.ruby-forum.com/u/daniel304)\
**Post date:** [October 14, 2009, 1:52pm UTC](https://www.ruby-forum.com/t/sophisticated-routes-recognition-with-regexp-for-instance-is-there-any-way/177278/1 "2009-10-14T13:52:25Z")

</div>

Hi!

Let’s imagine we need to deal with such non-RESTful routes:  
/!some\_value # note the ‘!’ in the beginning  
/resource(value)

== The first obvious way to do it in Rails is:

map.first\_example ‘/%21:value’, :controller =\> …  
map.second\_example ‘/resource%28:value%29’

(%21 is ‘!’, %28 and %29 are ‘(’ and ‘)’ respectively)

This won’t work:  
ActionController::RoutingError (No route matches “/resource%28foo%29”  
with {:method=\>:get}):

== Another approach is to use :requirements

map.first\_example ‘/:value’, :controller =\> 'first, :action =\>  
‘index’, :requirements =\> { :value =\> /%21(\w+)/ }

but it will route to controller with:  
Parameters: {“value”=\>"!foo"}  
and I will need to parse it again in the action!

second example is even more ugly:  
map.second\_example ‘:value’, :controller =\> ‘second’, :action =\>  
‘index’, :requirements =\> { :predicate =\> /resource%28(.\*)%29/ }

You see? I have to match this route entirely in :requirements and I  
still have:  
Parameters: {“value”=\>“resource(foo)”}  
in controller and have to parse it again.

== Questions:

1. Is there any alternative router for Rails and can I replace native  
router with it?

2. Is there common way to do so:

ActionController::Routing::Routes.draw do |map|  
… # regular routes  
map.any\_other\_route ‘:query\_string’, :controller =\>  
‘additional\_router’, :action =\> ‘parse\_route’ # this action can raise  
RoutingError if there is no matches even in ‘sophisticated’ routes map  
end
