Forum: Ruby on Rails Question about "super dynamic" routes, like :reqs => {:id =>

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.
ivanvega (Guest)
on 2005-11-23 22:49
(Received via mailing list)
Hi,

I want my app to handle different possibilities for a path. I know I can
easily do this inside my app, as I just evaluate the generic path and
render the desired component.

However, I was wondering if this was possible directly in routes.rb

Suppose I want to handle these three kind of URLs:

/<state>/...
/<code>/...
/<name>/...

Then, maybe I could do something like:

map.connect ':state/*path_info', :controller => 'states', :requirements
=> {not State.find(:first, :conditions => 'name =
\'#{:state}\'').empty?}
...

That would supposedly look if there was a state with that name, and use
that controller if there was. I'd use a similar approach for the other
routes.

I haven't tried if it works yet (if it does I'd be extremely surprised
if my example worked!), but if it did, would you recommend doing
something like this or handling it directly inside a controller?

Thanks!

Ivan V.
technoweenie (Guest)
on 2005-11-23 23:21
(Received via mailing list)
On 11/23/05, Iván Vega R. <removed_email_address@domain.invalid> wrote:
> /<state>/...
> that controller if there was. I'd use a similar approach for the other
> routes.
>
> I haven't tried if it works yet (if it does I'd be extremely surprised
> if my example worked!), but if it did, would you recommend doing
> something like this or handling it directly inside a controller?
>
> Thanks!
>
> Ivan V.

I doubt it will work since you're passing :state (the symbol, not the
param value) to State.  Even if that did work the way you're wanting,
it would execute DB queries just to match routes.  I'd suggest doing
this in the controller.

--
rick
http://techno-weenie.net
This topic is locked and can not be replied to.