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?
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 }
Parameters with dots on the URI
精彩评论