Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve the controller name from a class name

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)?

like image 699
Backo Avatar asked Dec 05 '22 21:12

Backo


1 Answers

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.

like image 109
Christoph Petschnig Avatar answered Dec 30 '22 15:12

Christoph Petschnig