I want to convert the string
"Full Time"
to
"full_time"
When I using “Full Time”.underscore in irb, it prompt a error
NoMethodError: undefined method `underscore' for "Full Time":String
How can I fix it? Or is there any other way for me to get the above underscored result?
There is a rails helper method called underscore. If you fire your rails console (script/console) you will be able to use it:
so what i think that you should do is, remove space and then apply the method described above. Note that I added to_sym just to show that it is possible as well, but if you don’t need it, just remove it.
Note that it is a rails helper and not a ruby method from the String class. It will just works in the rails enviornment.