views:

184

answers:

2

Hi, is it possible to have the controller value in a rails named route as a parameter, which I can pass at runtime to direct the call to the proper controller?

e.g. map.ride 'ride' ,:controller => {some-way-of-specifying-a-parameter}, :action => 'ride'

then at runtime, I want to pass the controller name to which this call should be going. My actions are doing different things depending to which controller the call gets sent. thanks

+1  A: 

This would work:

map.ride 'ride/:controller', :action => 'ride'

/ride/first would call FirstController#ride and /ride/second would call SecondController#ride

Tomas Markauskas
thanks Tomas - however, how would I specify the name of the controller? using the above I would normally get the ride_path available to me, and I would want to do something like ride_path(:controller => 'first') . would that work?
not to worry, i have figured it out, thanks a lot, thats what i wanted
+1  A: 

Thomas' answer is correct, however if you want more flexibility in the URL format you can specify multiple routes and use route requirements by putting :requirements on each. The route will only match if the requirements are met. For example:

map.resources :rides, :path_prefix => '/:option', 
  :requirements => { :option => /one/ }, :controller => 'one'
map.resources :rides, :path_prefix => '/:option', 
  :requirements => { :option => /two/ }, :controller => 'two'

and then:

/one/rides will go to OneController

/two/rides will go to TwoController

mikej