Rails 3 Routing Parameters with Dots


By default, Rails 3 interprets a dot in a URL as a format specifier. For example, given the route

match "/foo/:search"

and the URL /foo/bar.baz, this would result in the following parameters being passed:

{ :search => "bar", :format => "baz" }

This may not be what you desire, so here is a quick workaround, using segment constraints:

match "/foo/:search", :constraints => { :search => /[^\/]*/ }

The constraint for the :search parameter now matches anything but a slash, which includes the dot, and Rails will no longer interpret it as a format.

Update: The same effect can be achieved by using dynamic segments and the same regular expression, like this:

match "/foo/:search", :search => /[^\/]*/

 

Thanks to this guy and this guy, who led me in the right direction.

This is a small update on a blog post which I wrote back on my old blog. Still need to find a way to move all those articles over to this blog without breaking the links.