What's New in Edge Rails: No More Formatted Routes

Posted by ryan
at 8:45 PM on Wednesday, November 26, 2008

This feature is scheduled for: Rails v2.3/3.0

Parallel to the addition of :except and :only options to Rails’ routing comes the removal of all those formatted_xxx named routes. Turns out these routes ate up a lot of memory and served minimal purpose.

So with this routes.rb definition:

1
2
3
ActionController::Routing::Routes.draw do |map|
  map.resources :articles
end

you’ll no longer have any of the formatted_xxx url helpers available. However, you will be able to get the same functionality by passing in a :format option to the base view helper methods:

1
2
3
4
# Old => New
formatted_article_path(article, :xml) => article_path(article, :format => :xml)
formatted_new_article_path(:json) => new_article_path(:format => :json)
# etc...

A minimal change that will have a big impact on the memory consumption of each of your Rails processes – especially if you’ve got a complex app with lots of routes.

If you’re on Edge Rails you’re going to start seeing deprecation warnings where you reference these formatted url helpers, so you’ve got some warning before you have to make the switch.

tags: ruby, rubyonrails

Comments

Leave a response

  1. jcNovember 26, 2008 @ 11:32 PM

    If those eat up so much memory, it sure would be nice to get ride of the _path helpers as well. Or at least be able to turn them off via a config.

  2. W. Andrew Loe IIINovember 27, 2008 @ 12:12 AM

    @jc I believe those, like most crazy methods in rails, are generated at run time. If you don’t ever call them, they won’t ever take up memory. This is how I understand find_all_by_some_column and friends work, haven’t tested it with the routing helpers to be 100% sure.

  3. JamalNovember 27, 2008 @ 05:11 AM

    Can anyone tell me how I can update to latest rails edge?

  4. Vesa NieminenNovember 27, 2008 @ 05:46 AM

    @Jamal Run rake rails:freeze:edge on your Rails app root and it will freeze the latest edge version to your app.

  5. ScottNovember 27, 2008 @ 09:42 AM

    Nice! This should cut down on the memory footprint of my thin instances considerably.

  6. JazNovember 27, 2008 @ 03:43 PM

    @Scott, That’s what she said.

  7. Janie ParishNovember 29, 2008 @ 03:11 AM

    Like everything else that is new in Rails this is great stuff!

  8. Paul@Green HomeNovember 30, 2008 @ 04:07 PM

    I like the way Rubyites are not afraid to make radical changes to code. I think this will eventually nip PHP in the bud.

  9. CarlDecember 15, 2008 @ 06:49 PM

    @Paul, I think one of the reasons they aren’t too afraid to make changes like this is installing old versions of Rails (for legacy apps) is pretty easy and the apps even say what version of rails they were developed with in the config/environment.rb. So if you were to use code that doesn’t work on the newest version, you can pretty easily find out what version to go back to to make it work again. Not to mention the freezing functions for the times you just gotta be sure.

    But even with that, PHP is well beyond the “bud” stage alreaqdy. Rails has made PHP development painful to me, but there are still a lot of jobs out there calling for PHP experience. 8^(

  10. jasonDecember 16, 2008 @ 07:42 AM

    i recommend this be backported via a gem or whatever to 2.2 since rails 2.3 probably won’t be out for a long time

  11. Brendan BaldwinFebruary 02, 2009 @ 04:55 PM

    @w. andrew loe iii – incorrect. Named route methods and recognizers are defined dynamically when environment loads so you pay the price for mapping them out whether they are used or not.

    This is a really great change. Having separate method names for the “formatted” routes always felt like an ugly hack anyways.