Handling URL parameters with routes

Hi,

Rails 2.3.4
Ruby 1.8.7

I’m having trouble handling parameters in routes. I want to be able to
handle “?x=1&y=2” style url parameters.

Here’s the route:

map.connect “schools/list/:country/:province/:city”,
:controller => “schools”,
:action => “list”,
:country => nil,
:province => nil,
:city => nil

This works:

rs.recognize_path “/schools/list/US”
=> {:controller=>“schools”, :action=>“list”, :country=>“US”}

This doesn’t - why not?

rs.recognize_path “/schools/list?country=US”

ActionController::RoutingError: No route matches “/schools/list?
country=US” with {}
from /home/dberger/Repositories/globe_village/vendor/rails/
actionpack/lib/action_controller/routing/recognition_optimisation.rb:
66:in `recognize_path’

I guess I thought Rails handled url parameters automatically. No?

Regards,

Dan

Hi,

You need to mention that as a separate route.
Just add this additional line in your routes…

map.connect “schools/list/:country/:province/:city”,
:controller => “schools”,
:action => “list”,
:country => nil,
:province => nil,
:city => nil

map.connect “schools/list”, :controller => “schools”, :action => “list”

regards,
Sur
http://rubyonrailshacks.com