C
César DÃaz
Hi,
I've spend several time trying to replace latin characters in strings.
What I want is something like this:
Given the word "camión", I would like to get the following string:
"camion".
I've found several solutions in the Internet but they only work in the
Rails console, and when I try to launch my Rails application on Mongrel
(I am using Netbeans) nothing is working and from "camión" I am getting
"camin" instead of "camion".
For example, I've tried with these snippets:
1.*********************************
def nice_slug(str)
accents = {
['á','à ','â','ä','ã'] => 'a',
['Ã','Ä','Â','À','Ã'] => 'A',
['é','è','ê','ë'] => 'e',
['Ë','É','È','Ê'] => 'E',
['Ã','ì','î','ï'] => 'i',
['Ã','ÃŽ','ÃŒ','Ã'] => 'I',
['ó','ò','ô','ö','õ'] => 'o',
['Õ','Ö','Ô','Ò','Ó'] => 'O',
['ú','ù','û','ü'] => 'u',
['Ú','Û','Ù','Ü'] => 'U',
['ç'] => 'c', ['Ç'] => 'C',
['ñ'] => 'n', ['Ñ'] => 'N'
}
accents.each do |ac,rep|
ac.each do |s|
str = str.gsub(s, rep)
end
end
str = str.gsub(/[^a-zA-Z0-9 ]/,"")
str = str.gsub(/[ ]+/," ")
str = str.gsub(/ /,"-")
str = str.downcase
end
2.*************************
"camión".parameterize
It throws the following error:
undefined method `normalize' for "cami�n:String
c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/inflector.rb:283:in
`transliterate'
c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/inflector.rb:262:in
`parameterize'
c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/core_ext/string/inflections.rb:106:in
`parameterize'
3.*************************************
"camión".mb_chars.decompose.scan(/[a-zA-Z0-9]/).join
It throws the following error:
undefined method `decompose' for "cami�n:String
I am using Ruby 1.8.6, Rails 2.2, Mongrel 1.1.5 and Windows XP.
Please, some help.
I've spend several time trying to replace latin characters in strings.
What I want is something like this:
Given the word "camión", I would like to get the following string:
"camion".
I've found several solutions in the Internet but they only work in the
Rails console, and when I try to launch my Rails application on Mongrel
(I am using Netbeans) nothing is working and from "camión" I am getting
"camin" instead of "camion".
For example, I've tried with these snippets:
1.*********************************
def nice_slug(str)
accents = {
['á','à ','â','ä','ã'] => 'a',
['Ã','Ä','Â','À','Ã'] => 'A',
['é','è','ê','ë'] => 'e',
['Ë','É','È','Ê'] => 'E',
['Ã','ì','î','ï'] => 'i',
['Ã','ÃŽ','ÃŒ','Ã'] => 'I',
['ó','ò','ô','ö','õ'] => 'o',
['Õ','Ö','Ô','Ò','Ó'] => 'O',
['ú','ù','û','ü'] => 'u',
['Ú','Û','Ù','Ü'] => 'U',
['ç'] => 'c', ['Ç'] => 'C',
['ñ'] => 'n', ['Ñ'] => 'N'
}
accents.each do |ac,rep|
ac.each do |s|
str = str.gsub(s, rep)
end
end
str = str.gsub(/[^a-zA-Z0-9 ]/,"")
str = str.gsub(/[ ]+/," ")
str = str.gsub(/ /,"-")
str = str.downcase
end
2.*************************
"camión".parameterize
It throws the following error:
undefined method `normalize' for "cami�n:String
c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/inflector.rb:283:in
`transliterate'
c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/inflector.rb:262:in
`parameterize'
c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/core_ext/string/inflections.rb:106:in
`parameterize'
3.*************************************
"camión".mb_chars.decompose.scan(/[a-zA-Z0-9]/).join
It throws the following error:
undefined method `decompose' for "cami�n:String
I am using Ruby 1.8.6, Rails 2.2, Mongrel 1.1.5 and Windows XP.
Please, some help.