2

Sometimes when customizing a large preexisting Rails platform (e.g. Spree) it would be nice to be able to look up routes by url. Something like:

rake routes URL=/preexisting/url

This obviously doesn't work but is there something like this available in Rails? I've tried looking up the documentation for rake routes but haven't been able to find anything.

Ryan Linton
  • 1,285
  • 1
  • 14
  • 20

2 Answers2

2

If you're using a Unix-based OS (Linux/OSX) you can use grep.

rake routes | grep /preexisting/url

What this does is takes the output of rake routes, passes (or "pipes") it to grep, which then looks for lines matching "/preexisting/url".

Intelekshual
  • 7,444
  • 1
  • 21
  • 28
  • Cool! That gets the job done but do you know if it's possible possible using just rake routes? – Ryan Linton Apr 22 '13 at 18:37
  • The only option you can pass to rake routes is `CONTROLLER=x`, where `x` is the name of the controller (as seen in the output of `rake routes`). This will show only the routes related to that controller. – Intelekshual Apr 22 '13 at 18:42
2

Two ways to do it:

rake routes | grep 'preexisting\/url'

Or if you have the pry-rails gem installed, you can do this in the Rails console:

show-routes -G '/preexisting/url'

If the URL has an id in or other parameter in it, you'll probably want to just leave that part out of the search string.

I haven't used it, but I've heard of a gem called sextant that will show your routes in a browser window. That might be a good way to look over them, and just use your browser's page search feature to look for urls there.

sockmonk
  • 4,195
  • 24
  • 40
  • 1
    Thanks for the info. I'll have to try pry out sometime. It looks really cool. For the time being I'll probably stick to the first method though. I'd heard of sextant and was actually considering that as a solution but I was looking for a way to accomplish this from the terminal. – Ryan Linton Apr 22 '13 at 20:03