~ Software Developer

Getting Your Rails 3 App’s Routes at Runtime

Recently on my side project I came across an interesting problem: how could I get all the route paths in my Rails 3 app at runtime? I needed a way to get this information because the app uses a record’s attribute to directly set the path to it through a friendly URL, such as http://universit.as/ruby, so if a user created a record called ‘dashboard’, that would conflict with the ‘dashboard’ path and always show the dashboard page instead of the intended record page. As you can see, this is a terrible security flaw if we consider more serious scenarios.

After looking at it for 5 seconds or so, we can immediately notice how easy it is to get the app’s routes:

all_routes = Rails.application.routes.routes

Although it seems like that loading the whole app’s route mapping would be slow, it actually isn’t because they’re only loaded once when the Rails environment is loaded (when the server starts), so you basically get this for free.

So after mapping that array to the route’s path (you can get other attributes too, check the Rails source link) and doing some string manipulation, I managed to get a pretty satisfying result:

You should probably use uniqueness for this use case as far I can tell and be very careful when adding new routes in the future and consider the pros and cons to using a prefix like http://reddit.com/r/rails

Yeah, adding routes in the future was my biggest concern because some record could already hold that route’s name, but I guess that’s solvable by just changing the slug used to define the record’s URL, it would suck to arbitrarily change something user-related (even if the chances are so low), but the trade-off of having easy urls is worth it.

Good call on the uniqueness thing, hehe, I thought it was already in there but I guess I missed it :P