I am trying to create a permalink as a behavior for some article titles, and I do not want to add a new db field for permalink. Therefore, I decided to write an assistant that converts the title of my article from:
"O" focoasă "a pornit cruciada, împotriva bărbaţilor zgârciţi" to "O-focoasa-a-pornit-cruciada-impotriva-barbatilor-zgarciti".
So far I have figured out how to replace spaces with a hyphen and remove other special characters (except -) using:
title.gsub(/\s/, "-").gsub(/[^\w-]/, '').downcase
I am wondering if there is another way to replace a character with another different character from just one call to the .gsub method, so I won’t need to bind the title.gsub ("ă", "a") methods for all the special UTF-8 characters of my localization.
I was thinking of creating a hash with all the special characters and their counterparts, but I still haven't figured out how to use variables with regular expressions.
I was looking for something like:
title.gsub(/\s/, "-").gsub(*replace character goes here*).gsub(/[^\w-]/, '').downcase
Thanks!
source share