Result of `rake routes` in ruby script

2019-01-27 09:01发布

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.

3条回答
聊天终结者
2楼-- · 2019-01-27 09:04

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

查看更多
霸刀☆藐视天下
3楼-- · 2019-01-27 09:06

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

查看更多
可以哭但决不认输i
4楼-- · 2019-01-27 09:16

Another option is the rake shell; I love it.

查看更多
登录 后发表回答