views:

70

answers:

1

I have a rails application that allows searches using longitude and latitude. I have added a 'pretty' route with:

map.connect 'stores/near/:longitude/:latitude', :controller => 'stores', :action => 'index'

This works for integer latitude and longitude values (http://localhost:3000/stores/near/-88/49) but fails for decimal values (http://localhost:3000/stores/near/-88.341/49.123) giving:

Routing Error

No route matches "/stores/near/-88/49.0" with {:method=>:get}

Any ideas how to use pretty URLs in rails with decimals?

+1  A: 

Use the :requirements => { :param_name => pattern_regex } param.

DECIMAL_PATTERN = /\A-?\d+(\.\d+)\z/.freeze
map.connect 'stores/near/:longitude/:latitude', 
  :controller => 'stores', :action => 'index',
  :requirements => { :longitude => DECIMAL_PATTERN, :latitude => DECIMAL_PATTERN }

http://stackoverflow.com/questions/2600207/parameters-with-dots-on-the-uri

clyfe
Thanks for the response, but it doesn't seem to work. I'm still getting the same message as before the update. When running 'rake routes' I get:GET /stores/near/:longitude/:latitude {:action=>"index", :controller=>"stores", :requirement=>{:longitude=>/\A-?\d+(\.\d+)\z/, :latitude=>/\A-?\d+(\.\d+)\z/}}
Kevin Sylvestre
:requirement *typo* it's :requirements
clyfe
Thanks! That helped! The final solution required modifying the regular expression to be: '/-?\d+(\.\d+)/' (the application complained that 'anchor characters are not allowed in routing requirements'). Really appreciate the solution and help!
Kevin Sylvestre