Rails adds the humanize() method for strings, which works as follows (from the Rails RDoc):
"employee_salary".humanize # => "Employee salary" "author_id".humanize # => "Author"
I want to go the other way. I have a “pretty” input from the user I want to “de-humanize” to write to the model attribute:
"Employee salary" # => employee_salary "Some Title: Sub-title" # => some_title_sub_title
Does the rails include any help for this?
Refresh
In the meantime, I added the following to app / controllers / application_controller.rb:
class String def dehumanize self.downcase.squish.gsub( /\s/, '_' ) end end
Is there a better place to put it?
Decision
Thank you, FD , for the link . I implemented the solution recommended there. In my config / initializers / .rb infection, I added the following at the end:
module ActiveSupport::Inflector
ruby-on-rails
irkenInvader May 14 '10 at 16:22 2010-05-14 16:22
source share