Dynamic (database driven) Routes Problems

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

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

Steve

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?

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

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

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

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