Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get a hyphenated class name from a resource in Rails?

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.

like image 764
Steven Avatar asked Dec 22 '22 09:12

Steven


1 Answers

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"
like image 96
eugen Avatar answered Apr 28 '23 07:04

eugen