Monday March 31, 2008

   routes vs to_param

I'm working on a website for a fella who draws comics. He wanted the url to be /comics/12-02-2008 (where 12-02-2008 was the date the comic was published). I tried for a long while, and finally got my routes to do :controller/:action/:published_on instead of the standard :id. I did this only for the comics controller, and all others had :controller/:action/:id.

I kicked myself when I found to_param!

class Comic < ActiveRecord::Base
    def to_param
        "#{id}-#{published_on}"
    end 
end

This allows me to convert the :id symbol to :published_on. Much nicer than the old crusty routes!



Back