In Ruby on Rails, what is a good way to get subclass's name in a base controller class?
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.
精彩评论