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

后端 未结 3 916
感动是毒
感动是毒 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:42

    There doesn't appear to be any such method in the Rail API. However, I did find this blog post that offers a (partial) solution: http://rubyglasses.blogspot.com/2009/04/dehumanizing-rails.html

    0 讨论(0)
  • 2020-12-13 08:45

    In http://as.rubyonrails.org/classes/ActiveSupport/CoreExtensions/String/Inflections.html you have some methods used to prettify and un-prettify strings.

    0 讨论(0)
  • 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
    0 讨论(0)
提交回复
热议问题