Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handle rails route with GPS parameter

I'd like to create a route in my rails app to handle a gps-coordinate parameter. The intention is to find restaurants near the 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!

like image 291
Chris Crown Avatar asked Apr 11 '11 12:04

Chris Crown


1 Answers

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+/}
like image 169
eveevans Avatar answered Nov 15 '22 10:11

eveevans