Rails adds a humanize()
method for strings that works as follows (from the Rails RDoc):
\"employee_salary\".humanize # => \"Employee salary\"
the string.parameterize.underscore
will give you the same result
"Employee salary".parameterize.underscore # => employee_salary
"Some Title: Sub-title".parameterize.underscore # => some_title_sub_title
or you can also use which is slightly more succinct (thanks @danielricecodes).
Employee salary".parameterize("_") # => employee_salary
Employee salary".parameterize(separator: "_") # => employee_salary