[rails] Formatted URLs in SimplyRESTful

It took me a bit to figure out how to use the formatted_XXX_url named URLs that the SimplyRESTful plugin generates for you. It’s not hard, it’s just not documented, either, and the code isn’t the clearest to read. In fact, I probably wouldn’t have noticed them if it wasn’t for Rick Olson’s Routing Navigator.

So if you have the normal URL for a user like this:

=> /users/1

You can say this instead, and it’ll give you the XML version:

formatted_user_url(@user, :xml)
=> users/1.xml

It’s quite simple, really.

By the way, if you want this to work for iCalendar format with .ics extension, you need to add this to your environment.rb, as it’s missing from Rails’ mime_type.rb (I’ll submit a patch once Trac is back in business):

Mime::EXTENSION_LOOKUP["ics"] = Mime::ICS


Committed the fix already ;)
Thanks, David. I'll just post patches here from now on, then. Who needs Trac, anyway ;)
By Lars Pind on Wed, Jul 26, 06 at 04:27 · Reply

Leave a comment