开发者

Handle rails route with GPS parameter

开发者 https://www.devze.com 2023-02-24 00:00 出处:网络
I\'d like to create a route in my rails app to handle a gps-coordinate parameter. The intention is to find restaurants near th开发者_JS百科e given position.

I'd like to create a route in my rails app to handle a gps-coordinate parameter. The intention is to find restaurants near th开发者_JS百科e given position.

This is were I started:

match "/restaurants/near/:lat/:lng(/:range)", :to => "restaurants#near", :as => "near", :constraints => {:range => /\d+/}

It seems the router has problems with float parameters, an url like /restaurants/near/53.0123/10.5678 isn't recognized. Do you have a solution or best practice for handling GPS coordinates in rails urls?

Thank you!


The problem is caused because Rails try to use the "dots" for search for the format (.:format) So, you can add some constraints to fix it, for example:

match "/restaurants/near/:lat/:lng(/:range)", :to => "restaurants#near", :as => "near", :constraints => {:lat => /\-?\d+(.\d+)?/, :lng => /\-?\d+(.\d+)?/ , :range => /\d+/}
0

精彩评论

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