# Dynamic (database driven) Routes Problems

**URL:** <https://www.ruby-forum.com/t/dynamic-database-driven-routes-problems/84007>\
**Category:** Rails\
**Created:** [January 31, 2007, 1:32pm UTC](https://www.ruby-forum.com/t/dynamic-database-driven-routes-problems/84007 "2007-01-31T13:32:12Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Paul\_Odeon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/838e76/32.png) [@Paul\_Odeon](https://www.ruby-forum.com/u/Paul_Odeon)\
**Post date:** [January 31, 2007, 1:32pm UTC](https://www.ruby-forum.com/t/dynamic-database-driven-routes-problems/84007/1 "2007-01-31T13:32:12Z")

</div>

Hi all,

I’m a relative Rails Noob developing a CMS using rails. I have  
designed it so that all the public urls are created using an admin  
tool and stored in a database. I then want to use routes to direct the  
visitor to the appropriate action of a single controller.

The way I see it there are three ways of doing this:

1. Use components - I’d rather not as they are slow and dont really  
represent what I am trying to do
2. Somehow extract the id of the page using the path from the database  
within routes.rb, use that to get the appropriate action - I cant  
figure out how to do this in routes.rb
3. Redirect to a single action which can then point to another action

- Sort of what I’m already doing but as far as I know there is no way  
for a rails action to ‘render’ another action

Here is the route I am using:

`
       map.connect '*path', :controller => "public", :action => "page"
`

And here is the page action in PublicController:

`
       def page
               @page = Page.find_from_path(params[:path])
```
           render :action => @page.template, :layout =>

```
@page.layout
end
`

As you can see I would like to define the template and the layout  
(actually should be the action) in the database but I just cant work  
out a way to do it.

I have spent a long time trawling the web for a possible solution but  
have found nothing so far. If anyone could help come up with a  
solution in any way I would be immensely grateful.

Regards

Paul Odeon

---

<div class="post-metadata">

**Author:** ![Paul\_Odeon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/838e76/32.png) [@Paul\_Odeon](https://www.ruby-forum.com/u/Paul_Odeon)\
**Post date:** [January 31, 2007, 1:39pm UTC](https://www.ruby-forum.com/t/dynamic-database-driven-routes-problems/84007/2 "2007-01-31T13:39:13Z")

</div>

You can do:  
render :template =\> @page.template

Steve

---

<div class="post-metadata">

**Author:** ![Paul\_Odeon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/838e76/32.png) [@Paul\_Odeon](https://www.ruby-forum.com/u/Paul_Odeon)\
**Post date:** [January 31, 2007, 1:57pm UTC](https://www.ruby-forum.com/t/dynamic-database-driven-routes-problems/84007/3 "2007-01-31T13:57:15Z")

</div>

As far as I know this will just render the template file as is without  
calling the action. I really need to call the action.

Are you sure this will work?

---

<div class="post-metadata">

**Author:** ![Paul\_Odeon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/838e76/32.png) [@Paul\_Odeon](https://www.ruby-forum.com/u/Paul_Odeon)\
**Post date:** [January 31, 2007, 2:19pm UTC](https://www.ruby-forum.com/t/dynamic-database-driven-routes-problems/84007/4 "2007-01-31T14:19:16Z")

</div>

> As far as I know this will just render the template file as is without  
> calling the action. I really need to call the action.  
> Hmm… ok. You can use send() to call the action, then use render to  
> render the actual template:

# run the template method

send(@page.template)

# render the page with the template

render :template =\> @page.template

However, although I don’t know the application you’re developing, I  
would suggest that you think about whether or not you can use other  
methods to achieve this. If you have template specific variables that  
are set in the controller actions, see if you can use helpers instead.

Steve

---

<div class="post-metadata">

**Author:** ![Paul\_Odeon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/838e76/32.png) [@Paul\_Odeon](https://www.ruby-forum.com/u/Paul_Odeon)\
**Post date:** [January 31, 2007, 3:37pm UTC](https://www.ruby-forum.com/t/dynamic-database-driven-routes-problems/84007/5 "2007-01-31T15:37:25Z")

</div>

Ok, so you can use send() to call another action within the same  
controller? Interesting…

The problem I have is that the application has to refer to the  
database to know which url maps to which action. I am currently  
investigating extending the routing to achieve this but I will  
investigate the send method.

Thanks for your help with this Steve…

---

<div class="post-metadata">

**Author:** ![Paul\_Odeon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/838e76/32.png) [@Paul\_Odeon](https://www.ruby-forum.com/u/Paul_Odeon)\
**Post date:** [January 31, 2007, 4:08pm UTC](https://www.ruby-forum.com/t/dynamic-database-driven-routes-problems/84007/6 "2007-01-31T16:08:29Z")

</div>

> Ok, so you can use send() to call another action within the same  
> controller? Interesting…  
> Yeah - controllers are just Ruby classes and actions are just  
> methods. my\_class.send(“my\_method”) will call ‘my\_method’ on  
> ‘my\_class’.

> The problem I have is that the application has to refer to the  
> database to know which url maps to which action. I am currently  
> investigating extending the routing to achieve this but I will  
> investigate the send method.  
> What’s wrong with the method you were using? i.e. load the page from  
> the \*route and use the data from that to call the correct template. I  
> think you may be in danger of overcomplicating things.

Steve

---

<div class="post-metadata">

**Author:** ![Paul\_Odeon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/838e76/32.png) [@Paul\_Odeon](https://www.ruby-forum.com/u/Paul_Odeon)\
**Post date:** [January 31, 2007, 6:23pm UTC](https://www.ruby-forum.com/t/dynamic-database-driven-routes-problems/84007/7 "2007-01-31T18:23:45Z")

</div>

Steve,

I’ve managed to get the send method working after a bit of trial and  
error. I discovered that you have to do a “return false” in the  
Controller method doing the send call.

While this isnt a perfect solution (I still think it should be handled  
in the routes) Its not bad at all and it lets me get on with coding.

Just to clarify, I needed to be able to store the action in the  
database, not just the template…that was the problem I was having.

Thanks again for your help

Paul
