如何在模块化控制器中放置过滤器之前?
我在一个模块中有几个控制器:
class SoapTest::DashboardController < ApplicationController
class SoapTest::TestCasesController < ApplicationController
等等。
我希望能够检查用户是否对模块具有某些权限,并且由于我没有上面的控制器继承的“父”控制器,所以我想将检查放在应用程序的过滤器之前。但我似乎无法获取模块名称:
在应用程序控制器中,我有:
before_filter :check_company_features
def check_company_features
puts controller_name
end
但controller_name只返回“仪表板”。我需要获得“SoapTest”条款
I have several controllers that are in a module:
class SoapTest::DashboardController < ApplicationController
class SoapTest::TestCasesController < ApplicationController
etc.
I want to be able to check if a user has certain permissions for a module, and since I don't have a "parent" controller where the above ones inherit, i thought to put the check in a before filter in applications. But I can't seem to get the module name:
in application controller, i have:
before_filter :check_company_features
def check_company_features
puts controller_name
end
but controller_name just returns "dashboard". I need to get the "SoapTest" clause
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请注意,您当前所说的模块实际上是命名空间。
controller_name
只返回类名(而不是完全限定名)的原因是 Rails 显式地去除了命名空间。您可以通过调用控制器类上的 Ruby#name
方法来获取它们。您可以在
#name
上调用多种字符串变形方法来获取格式化版本。但是,我强烈鼓励您使用命名空间主控制器。
而不是使用
您可以扩展
SoapTest::ApplicationController
Be attention, what you currently call
modules
actually arenamespaces
.The reason why
controller_name
returns only the class name (and not the fully qualified name) is because Rails explicitly strips the namespaces. You can get them by calling the Ruby#name
method on the controller class.There are several String inflection methods you can call on the
#name
to get the formatted version.However, I strongly encourage you to use a namespaced main controller.
Instead of using
you can extend a
SoapTest::ApplicationController