Pavel K.
Pavel K.

Reputation: 6817

model name to controller name

How can I get the controller name out of the object if I don't know what the object is?

I am trying to do:

object.class.tableize

but Rails says:

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

I tried adding demodulize with same result.

thanks

Upvotes: 8

Views: 5602

Answers (2)

Gabriel Osorio
Gabriel Osorio

Reputation: 1063

For semantic reasons, you might want to do:

object.class.name #=> 'FooBar'

You can also use tableize with this sequence, like so:

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

I prefer it that way due to readability.

As well, note that tableize also does pluralization. If unwanted use underscore.

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

Upvotes: 1

bensie
bensie

Reputation: 5403

object.class.to_s.tableize

Upvotes: 20

Related Questions