2

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

mikej
  • 65,295
  • 17
  • 152
  • 131
user350785
  • 73
  • 2

2 Answers2

2

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

Community
  • 1
  • 1
mikej
  • 65,295
  • 17
  • 152
  • 131
1

This would work:

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

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

Tomas Markauskas
  • 11,496
  • 2
  • 33
  • 35
  • 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? – user350785 May 27 '10 at 09:53
  • not to worry, i have figured it out, thanks a lot, thats what i wanted – user350785 May 27 '10 at 14:31