:requirements Method for Rails 3 Routes

In Rails 2, if you wanted to have a parameter on a URL that had a period (e.g. a decimal point in latitude/longitude coordinates), you needed to use the :requirements method to tell Rails to include everything in the URL.

e.g.if your URL was /spots/new_popup/37.77617617425586,-122.39735126495361

… then the Rails 2 route would look like this:

map.connect '/spots/new_popup/:coords', :requirements => {:coords => /.*/}, :controller => 'spots', :action => 'new_popup'

In Rails 3, there :requirements method has been replaced with :constraints. So the route above would be re-written like this:

match '/spots/new_popup/:coords', :constraints => {:coords => /.*/}, :controller => 'spots', :action => 'new_popup'

Leave a Reply

Your email address will not be published. Required fields are marked *