In Rails, how do I go from a resource to a string containing its class name stylized with hyphens (as in a CSS class name)? I'd like a method #hyphenated_class_name
or the like that I can apply to ActiveRecord resource of, say, class MyResource
, and get back my-resource
.
You're probably looking for underscore
or dasherize
methods, from ActiveSupport::Inflector. You need both to go from a class name to hyphenated string:
> PrettyPrint::SingleLine.name.demodulize.underscore.dasherize
=> "single-line"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With