在 Ruby on Rails 中,在基本控制器类中获取子类名称的好方法是什么?
如果 ProductsController
和 CategoriesController
都继承自 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试
参阅文档了解更多详细信息。
Try
Refer to the documentation for more details.