如何从 Rails 3 中的视图/助手中检索控制器类?

发布于 2024-12-21 05:55:37 字数 164 浏览 4 评论 0原文

我知道 controller_name 返回包含控制器名称的字符串,但如何从助手中检索控制器类(或对象)?

编辑:当控制器具有命名空间时,该解决方案也应该有效(例如Admin::PostsController

I know about controller_name returning a string containing the controller's name but how can I retrieve the controller class (or object) from within a helper?

EDIT: The solution should also work when the controller is namespaced (eg. Admin::PostsController)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

薄荷→糖丶微凉 2024-12-28 05:55:37

您可以使用 constantize 方法,例如:

controller_name.constantize

虽然我不确定如果您有命名空间控制器,它会如何表现。

更新:

该方法不适用于所有控制器名称和/或命名空间。虽然可以将 #controller 方法与 #class 结合使用:

controller.class

You can use the constantize method, like:

controller_name.constantize

Though I'm not sure how it will behave if you have a namespaced controller.

Update:

That one won't work for all controller names and/or namespaces. Though one can use the #controller method in combination with #class:

controller.class
只想待在家 2024-12-28 05:55:37

视图可能不需要这样做。理想情况下,无论您在视图中尝试执行什么操作,都应该在控制器中执行。

尝试思考为什么要这样做,我能想到的最好答案是您想调用在控制器中定义的辅助方法。已经存在一个构造可以执行此操作,请使用 helper_method

对于几乎所有其他事情,控制器应该向视图提供该数据。不是将其从控制器中拉出的视图。 (例如,即使您不需要该类,控制器也可以为其提供 @controller_class = self.class,然后视图即可使用该类)

A view probably shouldn't need to do this. Ideally whatever you're trying to do in the view that expects this, you would instead do in the controller.

Trying to think about why you'd want to do this, the best answer I can think of is that you want to invoke a helper method you've defined in the controller. There already exists a construct to do this, use helper_method.

For pretty much anything else, the controller should provide that data to the view. Not the view pulling it out of the controller. (e.g. even though you shouldn't need the class, the controller could provide it with @controller_class = self.class, which would then be available to the view)

離殇 2024-12-28 05:55:37

在纯 Ruby 中,因为类名是常量,所以您可以这样做从字符串中获取类:

classname = 'Posts'
p Kernel.const_get(classname).methods

Rails 中有一个很好的快捷方式,constantize 就是为了这样:

p 'Posts'.constantize.methods

如果类名是例如“editable_file” ,首先调用 camelize 方法:

p 'editable_file'.camelize.constantize  # EditableFile
p 'extensions/editable_file'.camelize.constantize  # Extensions::EditableFile

编辑:如果您确实想获得未解调的控制器名称,则此代码位于config/initializers/controller_name.rb 应确保:

class ActionController::Metal
  def self.controller_name
    # @controller_name ||= self.name.demodulize.sub(/Controller$/, '').underscore
    @controller_name ||= self.name.sub(/Controller$/, '').underscore
  end
end

In pure Ruby, because class names are constants, you can do this to get the class from a string:

classname = 'Posts'
p Kernel.const_get(classname).methods

There is a nice shortcut in Rails, constantize for just this:

p 'Posts'.constantize.methods

If the classname is eg 'editable_file', first call the camelize method:

p 'editable_file'.camelize.constantize  # EditableFile
p 'extensions/editable_file'.camelize.constantize  # Extensions::EditableFile

EDIT: If you really want to get the controller name un-demodulized, then this code in config/initializers/controller_name.rb should ensure it:

class ActionController::Metal
  def self.controller_name
    # @controller_name ||= self.name.demodulize.sub(/Controller$/, '').underscore
    @controller_name ||= self.name.sub(/Controller$/, '').underscore
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文