Model name for controller name

How can I get the controller name from an object if I don’t know what this object is?

I am trying to do:

object.class.tableize 

but Rails says:

 undefined method `tableize' for #<Class:0xb6f8ee20> 

I tried to add demodulation with the same result.

thanks

+6
ruby-on-rails
source share
2 answers
 object.class.to_s.tableize 
+17
source share

For semantic reasons, you can do:

 object.class.name #=> 'FooBar' 

You can also use tableize with this sequence, for example:

 object.class.name.tableize #=> 'foo_bars' 

I prefer this because of readability.

Also, note that tableize also performs pluralization. If unwanted use is underscore .

Hope this helps anyone, even if it's an old thread :)

+1
source share

All Articles