Home > Uncategorized > Rails 1.2 routes and the dot “.”

Rails 1.2 routes and the dot “.”

Rails 1.2 came with great new features. One of them was the REST able applications. Other was the ability to serve results based on how the request was made. So you could send different results for controller/action.html, controller/action.xml or contoller/action.rss, for example. This one came with a price though: If you had any routes on your site that uses a dot (“.”), it will not work on rails 1.2.

Why is that? Well, the dot “.” is now a route separator, just like the slash “/”. But, of course, there’s a way to work around it if you don’t plan to use the dot for specific actions. Just place a :requirements => {:yourparam => /.*/} to your routes.rb. Here is an example:

Before:

# Here I could use an username like “john.smith” or “lys.cohen”, but not on Rails 1.2
map.connect “books/feed/:username”, {:controller => :book, :action => :book_feed}
After Rails 1.2:

# Now it works on Rails 1.2
map.connect “books/feed/:username”, {:controller => :book, :action => :book_feed, :requirements => { :username => /.*/}}

It worked great for me. And, best of all, it does not break the functionality, so you can use dots as they are intended to be used on other controllers and even actions on the same controller.

This tip was extracted from TextDrive Forums

Categories: Uncategorized Tags:
  1. August 29th, 2007 at 14:48 | #1

    Just what I was looking for, thanks!

  2. szeryf
    September 16th, 2008 at 13:27 | #2

    thanks! had the same problem with 2.1, now it works!

  1. No trackbacks yet.