在 Ruby on Rails 中,在基本控制器类中获取子类名称的好方法是什么?

发布于 2024-09-24 13:51:48 字数 432 浏览 1 评论 0原文

如果 ProductsControllerCategoriesController 都继承自 GenericController,那么获取字符串 products 的好方法是什么?当 URL 指向产品控制器时基类? (在这种情况下,基类的索引操作正在执行需要使用字符串products来查询数据库的操作)

可以使用self.class.to_s,并且它是Analytics::ProductsController,或者params[:controller]都可以使用,它是analytics/products,所以两者都可以用来提取子类的名称。使用其中一种方法比另一种更好,还是第三种方法更好?

If both ProductsController and CategoriesController both inherit from GenericController, what is a good way to get the string products in the base class when the URL is pointing to the Products controller? (in this case, the base class's index action is doing things that would need to use the string products to query the database)

self.class.to_s can be used, and it is Analytics::ProductsController, or params[:controller] can be used and it is analytics/products, so both can be used to extract the sub-class's name. Is using one better than the other, or is a third way even better?

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

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

发布评论

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

评论(1

始终不够 2024-10-01 13:51:48

尝试

controller_name

参阅文档了解更多详细信息。

Try

controller_name

Refer to the documentation for more details.

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