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


Let’s imagine we need to deal with such non-RESTful routes:
/!some_value # note the ‘!’ in the beginning

== 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