Forum: Ruby on Rails Dynamic (database driven) Routes Problems

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
0d408921ca63da8bdbeb8e0c7b807cf4?d=identicon&s=25 Paul Odeon (Guest)
on 2007-01-31 13:32
(Received via mailing list)
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:

<code>
       map.connect '*path', :controller => "public", :action => "page"
</code>

And here is the page action in PublicController:

<code>
       def page
               @page = Page.find_from_path(params[:path])

               render :action => @page.template, :layout =>
@page.layout
       end
</code>

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
6d57db44c5a4e0721cc7f78cf0ce308a?d=identicon&s=25 Stephen Bartholomew (steveb)
on 2007-01-31 13:39
(Received via mailing list)
You can do:
render :template => @page.template

Steve
0d408921ca63da8bdbeb8e0c7b807cf4?d=identicon&s=25 paulodeon@googlemail.com (Guest)
on 2007-01-31 13:57
(Received via mailing list)
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?
6d57db44c5a4e0721cc7f78cf0ce308a?d=identicon&s=25 Stephen Bartholomew (steveb)
on 2007-01-31 14:19
(Received via mailing list)
> 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
0d408921ca63da8bdbeb8e0c7b807cf4?d=identicon&s=25 paulodeon@googlemail.com (Guest)
on 2007-01-31 15:37
(Received via mailing list)
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...
6d57db44c5a4e0721cc7f78cf0ce308a?d=identicon&s=25 Stephen Bartholomew (steveb)
on 2007-01-31 16:08
(Received via mailing list)
> 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
0d408921ca63da8bdbeb8e0c7b807cf4?d=identicon&s=25 paulodeon@googlemail.com (Guest)
on 2007-01-31 18:23
(Received via mailing list)
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
This topic is locked and can not be replied to.