Heisenbug

April 8, 2008

Ruby on Rails: Routing and Periods/Full Stops

Filed under: Uncategorized — by jenf @ 3:40 pm

One of the things about rails that I find really well designed is the routing (mapping of url’s to controller and actions). However I have noticed a little problem, by default it assumes that the routes cannot have full stops in them, which is a bit of a pain with floating point numbers or files with extensions in them.

I want

location/-15.0/-45.0

To map to controller x and action y with two arguments latitude and longitude
I added the route below and found that it’d construct the link but not recognise it


map.connect "location/:lat/:lon",:controller=>"x",:action=>"y"

After a few quick tries I found it was due to the full stop and the easiest method I’ve seen is to add conditions to the routes and set it to


map.connect "location/:lat/:lon",:controller=>"x",:action=>"y",:requirements => {:lat => /\-?[0-9]*\.[0-9]*/,:lon => /\-?[0-9]*\.[0-9]*/

Generally speaking the routing mechanism is really good, allowing simple validation using regex’s to easily screen out dodgy requests.

No Comments Yet »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

Powered by WordPress.com