开发者

In Ruby on Rails, what is a good way to get subclass's name in a base controller class?

开发者 https://www.devze.com 2023-01-17 02:52 出处:网络
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 contro

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 act开发者_如何学Goion 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?


Try

controller_name

Refer to the documentation for more details.

0

精彩评论

暂无评论...
验证码 换一张
取 消