开发者

Rails Pretty URL with Decimals

开发者 https://www.devze.com 2022-12-27 14:58 出处:网络
I have a rails application that allows searches using longitude and latitude. I have added a \'pretty\' route with:

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号