Does rails have an opposite of 'humanize' for strings?

后端 未结 3 925
感动是毒
感动是毒 2020-12-13 08:34

Rails adds a humanize() method for strings that works as follows (from the Rails RDoc):

\"employee_salary\".humanize # => \"Employee salary\"         


        
3条回答
  •  执笔经年
    2020-12-13 08:57

    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).

    • Rails < 5 Employee salary".parameterize("_") # => employee_salary
    • Rails > 5 Employee salary".parameterize(separator: "_") # => employee_salary

提交回复
热议问题