69

[update: by not using rake routes, just to understand Rails console a little more]

It seems like inside of "rails console" for Rails 3, we can use controller, but in Rails 2.2 or 2.3, we need to use @controller

And in Rails 3, we can print out all the routes added by Rails routing for a scaffold foo:

ruby-1.9.2-p0 > puts controller.public_methods.grep(/path|url/).grep(/foo/).sort.join("\n")
edit_foo_path
edit_foo_url
foo_path
foo_url
foos_path
foos_url
new_foo_path
new_foo_url

but on Rails 2.3.8, it gives a bunch of formatted_foos_path, etc, and gives nothing for Rails 2.2.2. How to make it print out for Rails 2.3.8 and 2.2.2?


Details for Rails 2.3.8:

ruby-1.8.7-p302 > puts @controller.public_methods.grep(/path|url/).grep(/foo/).sort.join("\n")
formatted_edit_foo_path
formatted_edit_foo_url
formatted_foo_path
formatted_foo_url
formatted_foos_path
formatted_foos_url
formatted_new_foo_path
formatted_new_foo_url
1
  • 6
    what makes the question deserve a "-1" vote? Commented Mar 17, 2011 at 22:48

4 Answers 4

95

Rails 3.x–6.x

Rails.application.routes.named_routes.helper_names

Rails 2.x

helpers = Rails.application.routes.named_routes.helpers

This will get you all the named route methods that were created. Then you can do helpers.map(&:to_s), and whatever regex you want to get your foo versions

6
  • 6
    Rails.application.routes.named_routes.helpers.select {|h| h.to_s =~ /some_search/ }
    – Dmytro
    Commented Aug 8, 2015 at 14:05
  • 17
    In Rails 5 you need helpers = Rails.application.routes.named_routes.helper_names Commented Apr 28, 2017 at 18:23
  • 8
    The helpers method has been removed in new versions of Rails. It's now helper_names This works in rails 5.1: Rails.application.routes.named_routes.helper_names
    – kbaribeau
    Commented Sep 7, 2017 at 16:04
  • 2
    In Rails 6 this prints: ["rails_info_properties_path", "rails_info_routes_path", "rails_info_path", "rails_mailers_path", "rails_info_properties_url", "rails_info_routes_url", "rails_info_url", "rails_mailers_url"]. Not helpful. Commented Dec 13, 2019 at 12:55
  • Edited the question to include more recent helper_names method. Commented Apr 23, 2021 at 23:18
31

or load up localhost_path/rails/info/routes in your browser.

1
  • 3
    Works for Rails 6. Commented Dec 13, 2019 at 12:57
8

Well in Rails 4, I use rake routes. Is it that you need?

2
  • 1
    rake routes does not always list the associated url helper method.
    – bitops
    Commented May 25, 2016 at 21:14
  • You cannot see the names of the *_url *_path helpers there. Commented Dec 13, 2019 at 12:52
2

Rails >= 6.1

I just upgraded an app to 6.1.4 and noticed that rake routes no longer works.

Instead we can run bin/rails routes

To get a complete list of the available routes in your application, visit http://localhost:3000/rails/info/routes in your browser while your server is running in the development environment. You can also execute the bin/rails routes command in your terminal to produce the same output.

Docs: https://guides.rubyonrails.org/routing.html#listing-existing-routes

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.