I am looking for a way to decide the routes based on a request parameter.For example i want to have route a request to web controller if it has params[:web] and to iPhone if it has params[:iphone]. Is it possible to do so keeping the names of the routes same but routing them to different controllers/actions depending upon the parameter?
views:
64answers:
3Is there a way to check the parameters and decide the routes based on the parameters in rails?
Possible if you define route(or a named route) like below in your routes.rb file
map.connect '/:controller/:action/:platform',:controller => 'some controller name',:action=>'some action'
if you handle this in your action, you can use like params[:platform]
Read more on named routes if you customize more on this. As far as your prob is concerned I hope the above code solves the problem
Expanding on @lakshmanan's answer, you can do this:
Add this to your routes.rb:
map.named_route '/:controller/:action/:platform'
In your views,
<%= link_to "Blah blah", named_route_path(:controller => "some_controller",
:action => "some_action",
:platform => "some_platform")
In your some_controller,
def some_action
if params[:platform] == "web"
#DO SOMETHING
elsif params[:platform] == "iphone"
#DO SOMETHING
else
#DO SOMETHING
end
end
Assuming that there is a very good reason to have one controller accept this action (if there is shared code... move it to a helper method or model, and use the user agent info or named routes to your advantage), check the parameter and redirect to the appropriate controller and action:
def some_action
# some shared code here
if params[:platform] == 'iphone'
redirect_to :controller => 'foo', :action => 'bar'
elsif params[:platform] == 'web'
redirect_to :controller => 'baz', :action => 'baq'
else
# default controller and action here
end
end
If you really really want the named route to map to different controllers, you'll need to hardcode the platform string:
map.connect '/foo/bars/:id/iphone', :controller => 'iphone',:action=>'some_action'
map.connect '/foo/bars/:id/web', :controller => 'web',:action=>'some_action'
UPDATE0
From here, you might want to try map.with_options(:conditions => ... )