Rails 3:以编程方式获取命名空间中的路由列表 [英] Rails 3: Get list of routes in namespace programmatically

查看:23
本文介绍了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?

在我的 Admin 命名空间中创建新控制器时,我经常犯从 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆