Rails漂亮的URL与小数

时间:2010-04-15 20:24:15

标签: ruby-on-rails

我有一个rails应用程序,允许使用经度和纬度进行搜索。我添加了一条“漂亮”的路线:

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

适用于整数纬度和经度值( http://localhost:3000/stores/near/-88/49 ),但十进制值( http://localhost:3000/stores/near/-88.341/49.123 )无法给出:

Routing Error

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

如何在带有小数的rails中使用漂亮的URL?

1 个答案:

答案 0 :(得分:1)

使用: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