Iván Vega Rivera
2005-Nov-23  20:49 UTC
Question about "super dynamic" routes, like :reqs => {:id => Thing.find(:id)}
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.
Rick Olson
2005-Nov-23  21:18 UTC
Re: Question about "super dynamic" routes, like :reqs => {:id => Thing.find(:id)}
On 11/23/05, Iván Vega Rivera <ivanvega-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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.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