Ran
Ran

Reputation: 3523

Rails different route if parameter is present

Small routing question:

I want

any ideas how to change the routs file (in rails 2.3)

Upvotes: 0

Views: 1023

Answers (1)

Samiron
Samiron

Reputation: 5317

Rails 2.3.1:

map.route_a 'my_controller', :controller => "A", :action => "a"
map.route_b 'my_controller/uid/:uid', :controller => "B", :action => "b"

You should get route_a_url and route_b_url(:uid => uid) helper method to generate urls. Its not exactly query parameters but its using uid and a value both.

Rails 3.2.1:

match 'my_controller' => 'A#a', :as => 'route_a'
match 'my_controller/uid/:uid' => 'B#b', :as => 'route_b'

And the helper functions route_a_url and route_b_url(:uid=>10) were readily available.

Explnation:


In your view, use the helper functions to generate urls

Helper Function: route_a_url()
Generated URL: http://localhost:3000/my_controller
Map to: Controller A, Action a

Helper Function: route_b_url(:uid => 10))
Generated URL: http://localhost:3000/my_controller/uid/10
Map to: Controller B, Action b

Upvotes: 2

Related Questions