如何在模块化控制器中放置过滤器之前?

发布于 2024-10-22 02:21:44 字数 464 浏览 3 评论 0原文

我在一个模块中有几个控制器:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

潜移默化 2024-10-29 02:21:44

请注意,您当前所说的模块实际上是命名空间。

controller_name 只返回类名(而不是完全限定名)的原因是 Rails 显式地去除了命名空间。您可以通过调用控制器类上的 Ruby #name 方法来获取它们。

class SoapTest::DashboardController < ApplicationController
  before_filter :check_company_features

  def check_company_features
    puts controller_name
    # => "dashboard_controller"
    puts self.class.name
    # => "SoapTest::DashboardController"
  end 
end

您可以在 #name 上调用多种字符串变形方法来获取格式化版本。

但是,我强烈鼓励您使用命名空间主控制器。
而不是使用

class SoapTest::DashboardController < ApplicationController

您可以扩展 SoapTest::ApplicationController

class SoapTest::ApplicationController < ApplicationController
  before_filter :check_company_features

  def check_company_features
    # ...
  end
end

class SoapTest::DashboardController < SoapTest::ApplicationController
end

Be attention, what you currently call modules actually are namespaces.

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.

class SoapTest::DashboardController < ApplicationController
  before_filter :check_company_features

  def check_company_features
    puts controller_name
    # => "dashboard_controller"
    puts self.class.name
    # => "SoapTest::DashboardController"
  end 
end

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

class SoapTest::DashboardController < ApplicationController

you can extend a SoapTest::ApplicationController

class SoapTest::ApplicationController < ApplicationController
  before_filter :check_company_features

  def check_company_features
    # ...
  end
end

class SoapTest::DashboardController < SoapTest::ApplicationController
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文