Result of `rake routes` in ruby script

2019-01-27 09:18发布

问题:

rake routes is very slow (30s in my computer) but I need it for routing spec.

So, is there a way to get all routes like rake routes? (or how rake routes works?)

I use Rails 3 and all I have seen is for Rails 3, and I found nothing I can use in the rails doc.

回答1:

Rails.application.routes.routes.to_a

.to_a is optional, it just converts it to an array.

(I found this line in railties/lib/rails/tasks/routes.rake)

I use it like : routes[10].defaults => {:action=>"edit", :controller=>"polls"}

Edit : You can find the (quite hacky) way I do my routing specs here : https://gist.github.com/52ac6d848ce0d9fd52ac



回答2:

If you're using RSpec, you can use routing specs in your tests.



回答3:

Another option is the rake shell; I love it.