# Extending Routing

**URL:** <https://www.ruby-forum.com/t/extending-routing/71870>\
**Category:** Rails\
**Created:** [October 12, 2006, 1:53am UTC](https://www.ruby-forum.com/t/extending-routing/71870 "2006-10-12T01:53:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jhorner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/58956e/32.png) [@jhorner](https://www.ruby-forum.com/u/jhorner)\
**Post date:** [October 12, 2006, 1:53am UTC](https://www.ruby-forum.com/t/extending-routing/71870/1 "2006-10-12T01:53:11Z")

</div>

I am working on a CMS project and would like to extend how routing  
works, however I have not done such deep magic in the past.  
Specifically I would like to map the /\*path/ glob to a function that  
will return the controller, action and id based on a database lookup.  
I have it working now, but I am using render\_component to get it to  
work.

Any thoughts?

The current routing table:

ActionController::Routing::Routes.draw do |map|  
map.connect ‘:controller/service.wsdl’, :action =\> ‘wsdl’  
map.connect ‘:controller/:action/:id’  
map.connect ‘\*path’, :controller =\> ‘application’, :action =\>  
‘parse\_path’  
end

The current application controller:

class ApplicationController \< ActionController::Base  
model :domain  
attr\_writer :root\_node  
attr\_reader :root\_node  
before\_filter { |c| c.root\_node = Domain.find\_by\_name(c.request.host)  
if c.root\_node.nil? }

def parse\_path

```
# Set the current object to the root node for the current domain
obj = root_node

# Set the default action to index
action = 'render_node'

# Loop through each path element
for path_item in params[:path]

  # Find the new path element in the children of the current node
  new_obj = obj.children.find_by_name(path_item)

  # If the new path element is not found
  if new_obj.nil?
    # Then set the current path element to the action
    action = path_item
    # and exit the loop
    break
  end

  # Make the child the current node
  obj = new_obj
end

@obj = obj
logger.info( "obj: " + @obj.to_s )

node_controller = obj.class.to_s.underscore
logger.info( "node controller :" + node_controller )

# Render the node and action
render_component :controller => node_controller,
                  :action => action, :id => obj.id,
                  :obj => obj, :params => params

```

end  
end

While it is not pretty and I fear will not scale real well, it works.

---

<div class="post-metadata">

**Author:** ![jhorner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/58956e/32.png) [@jhorner](https://www.ruby-forum.com/u/jhorner)\
**Post date:** [October 12, 2006, 6:40am UTC](https://www.ruby-forum.com/t/extending-routing/71870/2 "2006-10-12T06:40:25Z")

</div>

On 10/12/06, Jeffrey H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 

I had a look at Radiant CMS recently and was somewhat disappointed about  
the  
way they implemented everything. They just had to rewrite most of the  
parts  
concerning template and layout rendering. They even have their own  
“controllers” (which they call behaviours). I think it’s a reasonable  
solution on its own, but there isn’t much to borrow if you want to build  
some small and cute CMS.

Anyway, take a look at Radiant’s implementation. Maybe you’ll find some  
interesting points to start from.

If you are really about to extend rails routing, take a look at Jamis  
Buck’s  
articles on it:  
[http://weblog.jamisbuck.org/2006/10/2/under-the-hood-rails-routing-dsl](http://weblog.jamisbuck.org/2006/10/2/under-the-hood-rails-routing-dsl)  
[http://weblog.jamisbuck.org/2006/10/4/under-the-hood-route-recognition-in-rails](http://weblog.jamisbuck.org/2006/10/4/under-the-hood-route-recognition-in-rails)

---

<div class="post-metadata">

**Author:** ![jhorner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/58956e/32.png) [@jhorner](https://www.ruby-forum.com/u/jhorner)\
**Post date:** [October 12, 2006, 6:52am UTC](https://www.ruby-forum.com/t/extending-routing/71870/3 "2006-10-12T06:52:28Z")

</div>

On 10/11/06, Jeffrey H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I am working on a CMS project and would like to extend how routing  
> works, however I have not done such deep magic in the past.  
> Specifically I would like to map the /\*path/ glob to a function that  
> will return the controller, action and id based on a database lookup.  
> I have it working now, but I am using render\_component to get it to  
> work.

That’s similar to what I’m doing in Mephisto, without the  
render\_component overhead (I just call a dispatch\_\* action in the same  
controller). With page caching, the performance hit doesn’t really  
matter.

–  
Rick O.  
[http://weblog.techno-weenie.net](http://weblog.techno-weenie.net)  
[http://mephistoblog.com](http://mephistoblog.com)
