从类名中检索控制器名称

发布于 2024-11-19 05:54:41 字数 276 浏览 2 评论 0原文

我正在使用 Ruby on Rails 3.0.7,我想检索给定类名的控制器名称。也就是说,我已经

Articles::Category

并且想要检索

articles/categories

我想要检索与 Articles::Categories 控制器无关的视图文件内的控制器名称。

我该如何做到这一点(可能使用一些 Ruby on Rails 核心方法)?

I am using Ruby on Rails 3.0.7 and I would like to retrieve a controller name given a class name. That is, I have

Articles::Category

and I would like to retrieve

articles/categories

I would like to retrieve the controller name inside a view file not related to the Articles::Categories controller.

How can I do that (possibly using some Ruby on Rails core method)?

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

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

发布评论

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

评论(5

痴意少年 2024-11-26 05:54:41
Articles::Category.name.underscore.pluralize
=> "articles/categories"

正如许多其他人提到的,没有特殊的方法。但只要您遵循 Rails 约定,这种方法就会带您走得更远。

Articles::Category.name.underscore.pluralize
=> "articles/categories"

As mentioned by many others, there is no special method for that. But as long as you followed Rails conventions, this approach will take you far.

画▽骨i 2024-11-26 05:54:41

要获取控制器名称,例如在视图内,您可以执行以下操作:

<%= controller.controller_name %>

要获取类的名称,例如 User,如果您有一个名为 user 的用户对象:

user.class.to_s 

除此之外,我认为控制器之间没有相关性和一个模型,它可以从类名中为您提供控制器名称,因为它们是不同的东西。您也许可以创建一个像 {:controller =>; 一样的哈希值。 'class_name'} 并自己映射它们。

To get the controller name, say inside your view, you can do :

<%= controller.controller_name %>

To get the name of a class, say User, if you have a user object named user :

user.class.to_s 

Other than that, i don't think there's a correlation between a controller and a model, that can give you the controller name from a class name, because they are different things. You can maybe create a hash like {:controller => 'class_name'} and map those yourself.

故事↓在人 2024-11-26 05:54:41
Articles::Categories.name.underscore #=> articles/categories

它强调驼峰式单词,以便:

RailsAdmin::ApplicationHelper.name.underscore #=> rails_admin/application_helper
Articles::Categories.name.underscore #=> articles/categories

And it underscores camelcased words to so that:

RailsAdmin::ApplicationHelper.name.underscore #=> rails_admin/application_helper
〃安静 2024-11-26 05:54:41

假设您真正的意思是 Articles::CategoriesController 那么 Articles::CategoriesController.controller_path 将为您提供您想要的内容。

更新 问题是,给定 Rails 模型的名称,如何获取关联控制器的名称?

这个问题的答案是,你不能。模型名称到控制器名称不存在一对一的映射。 User 模型可以由 users_controller.rb 和/或 admin/users_controller.rb 控制,或者根本没有关联的控制器。您当然可以根据 Rails 约定猜测一些可能的可能性,但您无法知道。

Assuming that you really meant Articles::CategoriesController then Articles::CategoriesController.controller_path will give you what you want.

Update The question is, given the name of a Rails Model, how do you get the name of the associated controller?

The answer to that question is, you can't. There is not a one-to-one mapping from the model name to the controller name. The User model could be controlled by the users_controller.rb and/or admin/users_controller.rb or not have an associated controller at all. You can certainly guess at some likely possibilities based on Rails conventions but you can't know.

又怨 2024-11-26 05:54:41

而不是 underscore.pluralize 只需使用 表化

ActiveRecord::Base.name.tableize
=> "active_record/bases"

Instead of underscore.pluralize just use tableize:

ActiveRecord::Base.name.tableize
=> "active_record/bases"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文