Check out your routes

— July 3, 2007 at 03:28 PDT

Well I finally got off my keister and wrote up a patch for a feature a few of us have been using for a while. If you're running edge Rails, just svn up and type rake routes to see a list of all the routes defined for your app.

If you're running an older version of Rails (1.2.3 works, not sure about pre-1.2 though) but still want in on the action, you should install sake and then pour yourself a shot:

$ sake -i routes
$ sake routes

I've found this task indispensable when dealing with RESTful routing. If you're doing anything beyond the basics with routes, you should check this out. It can be a huge time saver.

And in closing, a little history and credit. While the patch was mine, the inspiration came from elsewhere. First there was Rick Olson's routing_navigator plugin, which I found both very useful and way too awkward to use effectively. I cobbled up some code to do that from a script, while at the same time Chris Wanstrath did the obvious thing and wrote a rake task for it. I fixed that to show routes with names. And at last I got tired of the ugly formatting and fixed it up to the current incarnation. Just goes to show that software evolution beats intelligent designers. And if a tiny little patch has even that amount of history, just think about how some of the more complicated features may have developed.

14 commentsrails

  1. Robert2007-07-03 05:13:33

    Actually, it isn't "rake rails:routes" but rather "rake routes". Nice job on getting this in to the core, I love it!

  2. Rolf Bjaanes2007-07-03 09:16:45

    Didn't get it to work with 'rake routes' or 'rake rails:routes'. Did however work with 'sake routes' :)

    Running Rails 1.2.3

  3. Don2007-07-03 09:57:17

    Works like a charm, nice addition! Thanks

  4. Graeme Mathieson2007-07-03 12:01:29

    Ooh. runs a piston update Oh, wow, that's just lovely! Thank you!

  5. Mike2007-07-03 15:02:36

    Great timing! I've been looking for something like this for the past couple of weeks. Thank you!

  6. Mislav2007-07-03 15:15:09

    I've seen the commit, loved it, and only 5 hours later submitted this ;)

  7. Josh Susser2007-07-03 15:53:33

    Robert: Oh, looks like David hoisted the task up out of the rails namespace to the top level when he applied the patch. I didn't even notice! Fixed the article.

    Mislav: That's pretty cool! If you want to see my other experiment, look in that same pastie for a task that dumps the routes to a HTML table with some formatting.

  8. Robert Evans2007-07-03 17:00:30

    @Josh: David pulled a fast one. :D I used to use Rick's routing navigator all the time, but it is annoying to install, so I was really happy to see this committed to rails core!

  9. Mislav2007-07-03 17:56:36

    routes2HTML, sweet :) but the HTML is much more rich, surely the output can be enhanced... accepting cool patches for this?

  10. Josh Susser2007-07-03 23:05:24

    Mislav: I say let a thousand flowers bloom. Anyone who wants to take my little HTML experiment and do something snazzy with it has my blessing.

  11. Bill Bishop2007-07-04 16:32:33

    One thing I'd like to see added is a list of the path/url helpers - I haven't figured out how to pry them out of ActionController::Routing::Routes.named_routes.helpers nicely.

  12. Josh Susser2007-07-04 20:16:25

    Bill: The leftmost column shows the names of the routes. Just add the _path or _url suffix to get the helper method name.

  13. Brett2007-07-06 14:58:18

    Awesome! This rocks. Thanks

  14. EmmanuelOga2007-09-13 17:42:44

    Hi Josh! Thank you for your task. here is a modification with nicer output.:


Sorry, comments for this article are closed.