[ACCEPTED]-simple regex -- replace underscore with a space-string
str.gsub!(/_/, ' ')
gsub stands for 'global substitution', and the 4 exclamation means it'll change the string 3 itself rather than just return the substituted 2 string.
You can also do it without regexes 1 using
str.tr!('_', ' ')
On rails you can use the simplier
.humanize and ruby's 4
.downcase method but be careful as it also strips 3 any final '_id' string (in most cases this 2 is just what you need, even the capitalized 1 first letter)
'text_string_id'.humanize.downcase => "text string"
Whoops, I actually had it working--just 2 forgot to update the variable name :P
I was 1 using this:
@id = params[:id] @title = @id.gsub("_", " ")
Using split and join in rails
More Related questions