Search code examples
ruby-on-railsstringruby-on-rails-3classtypeconverter

How to convert the name of Class into a string in RoR


I have a variable called @class = Book. Bookbeing the constant representing the class.

I want to convert it to a string so i can use it in creating instant variable used in the views

In other words, i want the opposite method of constantize which can take a string and convert it into a class.

Any clues?

Thanks!


Solution

  • You can get name of a class by calling its name method:

    @class.name