Friday, December 26, 2008

How to Configure Rails Routes Containing a Dot, Period!

I had trouble matching a rails route containing a dot, like the following:
I created a route in routes.rb defined to match a radio station by frequency:
map.connect 'stations/:frequency', :controller => 'stations', :action => :show
Which I thought would do the trick, but instead I get the dreaded:
No route matches "/stations/103.3" with {:method=>:get}
But it works without the dot (for AM stations)
What gives?

The problem, I found in Rails bug reports, is that this is desired behavior. The dot is, by default considered separator, just like a slash. One way to fix this is to specify the pattern for frequency as anything which is not a slash:
map.connect 'stations/:frequency', :controller => 'stations', :action => :show, :frequency => /[^\/]+/
Which does the trick

No comments: