Rails 3:以编程方式获取名称空间中的路由列表 [英] Rails 3: Get list of routes in namespace programmatically
本文介绍了Rails 3:以编程方式获取名称空间中的路由列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何获取 Admin
名称空间中所有路由的列表,以便可以
How can I get a list of all the routes in my Admin
namespace so that I can use it in one of my tests?
我经常犯从<$ c继承的错误在我的 Admin
名称空间中创建新控制器时,使用$ c> ApplicationController 而不是 AdminController
。因此,我想编写一个访问我的 Admin
命名空间中的所有路由并验证每个路由都需要登录用户的测试。
I frequently make the mistake of inheriting from ApplicationController
instead of AdminController
when creating new controllers in my Admin
namespace. So, I want to write a test that visits all the routes in my Admin
namespace and verifies that each one requires a logged in user.
推荐答案
test_routes = []
Rails.application.routes.routes.each do |route|
route = route.path.spec.to_s
test_routes << route if route.starts_with?('/admin')
end
这篇关于Rails 3:以编程方式获取名称空间中的路由列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文