Conditional routes

In routes.rb I have:

map.connect ‘’, :controller => ‘stories’

However, if I want to do the following:

if CONDITION
map.connect ‘’, :controller => ‘stories’
else
map.connect ‘’, :controller => ‘others’
end

Is this possible? I want certain conditions to be evaluated before
deciding where to route it? Or is there a better way of doing this?

Thank you,

Sharkie

Hi !

2006/10/31, Sharkie L. [email protected]:

if CONDITION
map.connect ‘’, :controller => ‘stories’
else
map.connect ‘’, :controller => ‘others’
end

routes.rb will be evaluated only once in production. So, your
condition will not be processed as you intend.

Instead, you need to have a single action that will either redirect /
render the expected layout. Something like this:

routes.rb

map.connect ‘’, :controller => ‘take_a_decision’, :action => ‘choose’
map.stories ‘stories/:action/:id’, :controller => ‘stories’
map.others ‘others/:action/:id’, :controller => ‘others’

take_a_decision.rb

class TakeADecision < ApplicationController
def choose
if CONDITION
redirect_to(stories_url)
else
redirect_to(others_url)
end
end
end

Alternatively:
class TakeADecision < ApplicationController
def choose
if CONDITION
render(:action => ‘stories’)
else
render(:action => ‘others’)
end
end
end

Hope that helps !

François Beausoleil
http://blog.teksol.info/
http://piston.rubyforge.org/

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs